Home > database >  The exists multi-table query problem
The exists multi-table query problem

Time:10-09

Bosses, I associated database table A and table B through uid, I want to through the exists now, at the same time query A id, B the name field, could you tell me how to write the SQL can be...

 
The select Anderson d
//, b.n ame - how to write here...
The from tableA a
WHERE the EXISTS (
SELECT the name FROM tableB b
WHERE b.u id=a.u id)

CodePudding user response:

If you want to take B table fields, or use the JOIN syntax
The SELECT Anderson D, B.N AME
The FROM tableA A
The JOIN tableB B
ON A.u id=B.u id

CodePudding user response:

The select Anderson d, b.n ame from a, b where a.u id=b.u id

CodePudding user response:

Exists is used to judge, the value of the inside should be can't get, for example the exists inside the select the name and select 1 no difference

CodePudding user response:

reference 1/f, three years 丿 response:
if you want to take B table fields, or use the JOIN syntax
The SELECT Anderson D, B.N AME
The FROM tableA A
The JOIN tableB B
ON A.u id=B.u id


See ali code, do not recommend using the join method,
Try to use the exists, if it exists to really have no didn't also way,

CodePudding user response:

 SELECT A.u id, B.n ame 
The FROM tableA A, (SELECT the name FROM tableB) B
WHERE the EXISTS (SELECT the name FROM tableB BS
WHERE BS. The uid=A.u id AND BS. Name=B.n ame);

This way the Exists?

CodePudding user response:

1 ali only disable more than 3 table join, but if there is no ali the amount of data that completely don't have to worry about this

There is no much difference in 2 exists and join the execution plan, the join is mysql to choose driver table, the exists is good SQL specified directly, if not selected, or use the join
  • Related