[英]select rows where values equal to column values in MySQL
我有一个表Table 1 ,其中有5列是这样的
| ID | Name | V1 | V2 | V3 |
| 1 | A | 103 | 507 | 603 |
| 2 | B | 514 | 415 | 117 |
| 3 | C | 741 | 895 | 854 |
另一个表Table 2具有这样的值
| Values | Rooms |
| 103 | ABC |
| 507 | DEF |
| 603 | GHI |
| 514 | JKL |
| 415 | MNO |
| 117 | PQR |
我想从表2中选择房间,其中值等于表1中多行的V1,V2和V3。简而言之,我想选择V1,V2和V3列的值,例如:
103, 507, 603, 514, 415, 117, 741, 895, 854
因此我可以在表2的select语句中运行where子句。
您可以尝试以下一种方法:
SELECT t2.[Values] FROM Table2 t2
INNER JOIN Table1 t1 ON t1.V1 = t2.[Values]
OR t1.V2 = t2.[Values]
OR t1.V3 = t2.[Values]
试试这个:
SELECT t1.v1,t1.v2,t1.v3 FROM Table2 t2
INNER JOIN Table1 t1 ON t1.V1 = t2.[Values]
OR t1.V2 = t2.[Values]
OR t1.V3 = t2.[Values]
如果我对您的理解正确,那么您需要这样做:
select t2.Rooms from Table2 t2
where exists
select * from Table1 t1 where t1.V1 = t2.Values
or t1.V2 = t2.Values
or t1.V3 = t2.Values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.