简体   繁体   中英

Select all rows in table A if another table B has rows in mysql

In mysql database, I have two tables A and B and would like to select

  • all rows in table A if table B has any rows
  • no rows in table A if table B has no records

How should I write my SQL query?

Table A and B have columns id and name .

Just check in the where-condition, if b has any rows:

SELECT a.*
FROM a
WHERE EXISTS(SELECT 1 FROM b);

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