SQL query return data from multiple tables

前端 未结 6 955
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 04:23

I would like to know the following:

  • how to get data from multiple tables in my database?
  • what types of methods are there to do this?
  • what are
6条回答
  •  不知归路
    2020-11-21 05:18

    Part 3 - Tricks and Efficient Code

    MySQL in() efficiency

    I thought I would add some extra bits, for tips and tricks that have come up.

    One question I see come up a fair bit, is How do I get non-matching rows from two tables and I see the answer most commonly accepted as something like the following (based on our cars and brands table - which has Holden listed as a brand, but does not appear in the cars table):

    select
        a.ID,
        a.brand
    from
        brands a
    where
        a.ID not in(select brand from cars)
    

    And yes it will work.

    +----+--------+
    | ID | brand  |
    +----+--------+
    |  6 | Holden |
    +----+--------+
    1 row in set (0.00 sec)
    

    However it is not efficient in some database. Here is a link to a Stack Overflow question asking about it, and here is an excellent in depth article if you want to get into the nitty gritty.

    The short answer is, if the optimiser doesn't handle it efficiently, it may be much better to use a query like the following to get non matched rows:

    select
        a.brand
    from
        brands a
            left join cars b
                on a.id=b.brand
    where
        b.brand is null
    
    +--------+
    | brand  |
    +--------+
    | Holden |
    +--------+
    1 row in set (0.00 sec)
    

    Update Table with same table in subquery

    Ahhh, another oldie but goodie - the old You can't specify target table 'brands' for update in FROM clause.

    MySQL will not allow you to run an update... query with a subselect on the same table. Now, you might be thinking, why not just slap it into the where clause right? But what if you want to update only the row with the max() date amoung a bunch of other rows? You can't exactly do that in a where clause.

    update 
        brands 
    set 
        brand='Holden' 
    where 
        id=
            (select 
                id 
            from 
                brands 
            where 
                id=6);
    ERROR 1093 (HY000): You can't specify target table 'brands' 
    for update in FROM clause
    

    So, we can't do that eh? Well, not exactly. There is a sneaky workaround that a surprisingly large number of users don't know about - though it does include some hackery that you will need to pay attention to.

    You can stick the subquery within another subquery, which puts enough of a gap between the two queries so that it will work. However, note that it might be safest to stick the query within a transaction - this will prevent any other changes being made to the tables while the query is running.

    update 
        brands 
    set 
        brand='Holden' 
    where id=
        (select 
            id 
        from 
            (select 
                id 
            from 
                brands 
            where 
                id=6
            ) 
        as updateTable);
    
    Query OK, 0 rows affected (0.02 sec)
    Rows matched: 1  Changed: 0  Warnings: 0
    

提交回复
热议问题