子查询使用主查询中的值

时间:2015-03-04 07:24:07

标签: mysql sql jdbc

SELECT 
   userid, lastname, firstname , (SELECT COUNT(day) FROM PROFILE WHERE userid = ?) as numday
FROM PROFILE

我想知道每个用户在PROFILE表中出现的天数,但我不知道应该在查询中的问号(?)中放置什么值。

请帮忙......

2 个答案:

答案 0 :(得分:1)

试试这个

SELECT 
   userid, lastname, firstname ,
  (SELECT COUNT(day) FROM PROFILE WHERE userid = P.userid) as numday
FROM PROFILE P
使用GROUP BY

更新

SELECT userid, lastname, firstname, COUNT(day) as numday
FROM PROFILE
GROUP BY userid, lastname, firstname

答案 1 :(得分:0)

尝试这个未经测试的查询:

SELECT 
   userid, lastname, firstname , numday
FROM PROFILE as p join (SELECT COUNT(day) as numday, userid FROM DTR group by userid ) as q on p.userid = q.userid