选择在其他表中具有多种体验的用户

时间:2014-05-05 15:50:25

标签: mysql sql select

tblusers

+-------------+---------------+----------+
| id_users    | user          | password |
+-------------+---------------+----------+
| 1           | gurodrigues   | 1234     |
| 2           | lower52       | 123      |
| 3           | test55        | 1234     |
+-------------+---------------+----------+

tblexperiences

+----------------+-----------+-------------------+
| id_experience  | id_users  | detail            |
+----------------+-----------+-------------------+
| 1              | 1         | 'Cool, fantastic' |
| 2              | 1         | 'Nice!!'          |
| 3              | 2         | 'Boring'          |
+----------------+-----------+-------------------+

SELECT tblusers.id, tblusers.user from tblusers, tblexperiences WHERE tblexperiences.id = tblusers.id

如果我先执行此操作SELECT,它会返回很多重复的行,但我只需要一次,我该怎么办呢?

在另一个SELECT中我必须让tblexperiences上的所有用户WHERE有多行tblexperiences.id_users

1 个答案:

答案 0 :(得分:1)

SELECT tblusers.id, tblusers.user 
from tblusers
join tblexperiences ON tblexperiences.id = tblusers.id
group by tblusers.id, tblusers.user 

并且对于问题的第二部分,添加

having count(tblexperiences.id) > 1