简体   繁体   中英

condition in 2 table but select just from one table

I have two tables in a mysql database.

I need to select columns just from the first table and do the condition in the 2 table for example :

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 .

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM