Subconsulta MySql para extraer datos de otra tabla

I am currently using phpMyAdmin supporting MySql 5.0.

Tengo las siguientes dos tablas:

Master Facebook Insights
------------------------

Client ID  Client Name  Date        Top Likes By Gender
---------  -----------  ------      --------------------
1          Client1      2012-01-01  
1          Client1      2012-02-01
2          Client2      2012-01-01
2          Client2      2012-02-01
...etc. (the dates are always the beginning of each month for however many months
& clients exist)

Likes By Demographic
----------------

Date        Demographic1  Demographic2  Demographic3  Client
------      ------------  ------------  ------------  ------
0000-00-00  M.35-44       M.45-54       M.25-34       1
2012-01-01  53            44            28            1
2012-01-02  53            46            29            1
...etc.
0000-00-00  M.18-24       M.35-44       M.25-34       1
2012-02-01  374           221           194           1
2012-02-02  374           222           195           1
...etc.
0000-00-00  F.35-44       F.25-34       M.35-44       2
2012-01-01  194           182           83            2
2012-01-02  194           182           83            2
...etc.
0000-00-00  F.35-44       F.25-34       M.35-44       2
2012-02-01  196           180           83            2
2012-02-02  197           180           83            2
...etc. 

For the Likes By Demographic table: All days of each month are listed per client; when a new month begins, there is a new set of demographics that may or may NOT be listed exactly the same as the previous month. This is because the data was imported from a CSV that put the demographics in order of highest 'likes,' so this usually changes month to month. This is also the reason that the individual demographics are not the column headers (because they are inconsistent).

My problem is the following: I wish to list the top 3 demographics per month for each client in the 'Top Likes By Gender' column of the first table, like so:

Client ID  Client Name  Date        Top Likes By Gender
---------  -----------  ------      --------------------
1          Client1      2012-01-01  M.35-44, M.45-54, M.25-34 
1          Client1      2012-02-01  M.18-24, M.35-44, M.25-34
2          Client2      2012-01-01  F.35-44, F.25-34, M.35-44
2          Client2      2012-02-01  F.35-44, F.25-34, M.35-44

The use of subqueries is confusing me. The following is the (incorrect) code I have been trying to fix. The problem is that is just extracts the first three demographics for the first client (M.35-44, M.45-54, M.25-34) and repeats them down the entire column for all clients and dates. Am I on the right track, or is there a much simpler/more correct way to do this?

UPDATE `Master Facebook Insights`
SET `Top Likes By Gender` =
(select * from
(SELECT DISTINCT CONCAT(`Demographic1`,', ',`Demographic2`,', ',`Demographic3`)
FROM `Likes By Demographic` t1
JOIN `Master Facebook Insights` t2
ON t1.`Client` = t2.`Client ID`
WHERE  t1.`Date` = '0000-00-00'
AND t2.`Date` = 

(
SELECT MIN(`Date`)
FROM `Likes By Demographic`
WHERE `Date` > '0000-00-00'
AND `Client` = t2.`Client ID`
GROUP BY `Date`
ORDER BY `Date` ASC
LIMIT 1
)

)inner1 limit 1)

¡Muchas gracias por la ayuda!

preguntado el 12 de junio de 12 a las 21:06

Multivalued columns... you should probably revise your DB design... -

You should have a demographic lookup table that stores the possible values (e.g. M.35-44). -

praguian: Yes, I've already thought about that, but how would I go about implementing that?I apologize for the ignorance; I'm not experienced in databases. -

1 Respuestas

Just using the original Likes_By_Demographic table, you can do:

select
  client_id,
  date,
  concat(demo1,', ',demo2,', ',demo3) `top_likes`
from likes_by_demographic
group by client_id, date
order by client_id, date;

...which will yield (pseudo-data):

| CLIENT_ID |                            DATE |             TOP_LIKES |
-----------------------------------------------------------------------
|         1 |  January, 01 2012 00:00:00-0800 | M.35-44, M.25-34, 195 |
|         1 | February, 01 2012 00:00:00-0800 | M.25-34, M.35-44, 195 |
|         1 |    March, 01 2012 00:00:00-0800 |     M.25-34, 195, 169 |
|         2 |  January, 01 2012 00:00:00-0800 |     F.45-54, 210, 195 |
|         2 | February, 01 2012 00:00:00-0800 |      F.45-54, 210, 75 |

Respondido el 12 de junio de 12 a las 21:06

Hmmm...Because of the way my Likes By Demographic table is set up, this code actually pulls every individual day & associated "likes", such as: Date ----- 0000-00-00 // 2012-01-01 // 2012-01-02 // 2012-01-03 // etc. Top Likes ---------- M.35-44, M.45-54, M.25-34 // 53, 44, 28 // 53, 46, 29 // etc. ...rather than group by month. I don't understand how that would help. My main problem is the fact that demographics aren't being matched up correctly with each client+month. Bleh, sorry for my confusion... =/ - como leprosos

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.