Skip to content
Advertisement

MySQL Multiple Conditions in Grouped Data

I am trying to see how many users have clicked on an ad campaign and signed up afterwards.

My Logging table has the columns IP, UserID, and Data (this has the $_GET info like utm_source = newsletter). A user may click a link in newsletter and then later sign up on my website (which is indicated when they have a UserID that is not blank). I am trying to group by IP and then see if any IP has had an instance of utm in Data column and a non-blank UserID (but note that these 2 criteria may be true on different rows)

Currently I have:

However this is not correct because the WHERE statement filters entire rows, not within the grouped data.


Let me clarify this further.

If table looks like

I want the output to look like

Advertisement

Answer

Add a HAVING clause, which treats similar to a WHERE condition for an aggregation, principally in order to provide getting a concatenated non-null Data and UserID values at the same time per each ip value along with individually using GROUP_CONCAT() function such as

Demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement