How can I use FIND_IN_SET within a JOINED table?

后端 未结 1 614
情深已故
情深已故 2021-01-27 20:30

I have 2 tables:

  • user: names of registered blood donors with their blood type.
  • blood: contains blood types and blood com
1条回答
  •  后悔当初
    2021-01-27 21:03

    You need to put the find_in_set() in the ON clause:

    SELECT u.name AS name, b.name AS blood
    FROM user u JOIN
         blood b
         ON FIND_IN_SET(u.id_blood, b.receive) > 0;
    

    However, you should really have a separate table with one row per blood type and each type that can be received. This is called a junction table. Storing lists in comma-separated strings is not the SQL way of doing things: tables are.

    EDIT:

    Like this:

    SELECT u.name AS name, b.name AS blood
    FROM user u JOIN
         blood b
         ON FIND_IN_SET(u.id_blood, b.receive) > 0
    WHERE u.id_blood = 5;
    

    0 讨论(0)
提交回复
热议问题