[英]MySQL - Select specific rows in a table which are related to another table
我有两个表如下
表格1
Userid Name Gender Country
===================================================
u001 Name 1 Female IN
u002 Name 2 Male US
u003 Name 3 Male IN
u004 Name 4 Female UK
u005 Name 5 Female US
表 2
Userid UserRole Status
=======================================================
u001 r001 1
u002 r002 1
u003 r002 1
u004 r002 1
u005 r003 1
我想 select 表 1 中的所有行(使用join
是可选的)是女性并且userRole
为'r002'
。
我找不到合适的语法来做到这一点。
SELECT * FROM `table_1` WHERE `Gender` = 'Female' and `Userid` in (SELECT `Userid` FROM `table_2` where `UserRole` = 'r002')
SELECT t1.*
FROM `table_1` t1
INNER JOIN `table_2` t2 ON (t1.Userid = t2.Userid AND t2.UserRole = 'r002')
WHERE
t1.Gender = 'Female'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.