search

Home  >  Q&A  >  body text

What is the difference between two types of multi-table queries in MySQL?

SQL 1:

SELECT
    a.*,
    b.name
FROM
    t_coach a
LEFT JOIN t_school b on a.school_id=b.id

SQL 2:

SELECT a.*, (select b.name FROM t_school b WHERE b.id= a.school_id) AS name  FROM t_coach a

SQL1 and SQL2 get the same result.

Can the second SQL be written like this in actual development?
Compared with SQL1, is SQL2 less efficient?

为情所困为情所困2737 days ago577

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-18 10:46:43

    SQL1, table B is only scanned once
    SQL2, table B will be scanned as many times as there are records in table A
    When the amount of data in your database is enough, SQL2 will not be able to produce results directly, and will kill the database

    reply
    0
  • Cancelreply