Home  >  Q&A  >  body text

mysql cascade query

table A :suit
table B :suit_goods
Association conditions: suit.suit_id = suit_goods.suit_id
Corresponding relationship: A => Multiple B

SQL select How to achieve the above result? ? ? Ask God for enlightenment

Result:
When a record is associated with the suit_goods table, first this record is retained, and other associated data is displayed below

某草草某草草2675 days ago872

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-22 11:56:37

    select ...
    from suit s
    right join suit_goods g on s.suit_id = g.suit_id

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-22 11:56:37

    Looking at you, suit_goods should be used as the main table
    select ...
    from suit_goods g
    left join suit s on s.suit_id = g.suit_id

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-22 11:56:37

    SELECT
        `B`.*,
        `A`.`name`
    FROM
        `suit_goods` `B`
    LEFT JOIN
        `suit` `A`
    ON
        `A`.`suit_id` = `B`.`suit_id`;

    In this way, the data is basically the same as suit_goods, and an additional column of suit.name (this is written casually because I didn’t see the structure of your database) represents the name of the suit to which this good belongs. According to this, the field can be Play freely.

    reply
    0
  • Cancelreply