[英]condition in 2 table but select just from one table
I have two tables in a mysql database. 我在mysql数据库中有两个表。
I need to select columns just from the first table and do the condition in the 2 table for example : 我只需要从第一个表中选择列,然后在2个表中做条件,例如:
I have in the first table the columns: 我在第一张表中的列:
amount | date | name | address
and in the second I have: 在第二个中我有:
amount | date | cin | time
The condition would be WHERE amount = amount and date = date
. 条件为:
WHERE amount = amount and date = date
。
But select just the data from the first table. 但是,仅从第一个表中选择数据。 I dont need to display the data of the second table.
我不需要显示第二个表的数据。
You could use the EXISTS operator to find out whether a corresponding row in the second table exists: 您可以使用EXISTS运算符找出第二个表中是否存在对应的行:
SELECT * FROM first_table t1
WHERE EXISTS (
SELECT 1
FROM second_table t2
WHERE
t1.amount = t2.amount AND t1.date = t2.date
);
This ensures you won't have to use DISTINCT to reduce your resultset, if more than one row with your condition exists in the second table. 如果第二个表中存在符合条件的一行以上,则可以确保您不必使用DISTINCT来减少结果集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.