SQL Array Search

前端 未结 4 1566
悲&欢浪女
悲&欢浪女 2021-01-26 05:45

I have a group feature on my website that allows 1 user to put the userids of any of his or her friends into an invitedusers column. I need to do an SQL search to determine whet

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-26 06:25

    Assuming that you're really using 234,394,479 as value of one column (you at least should use ,234,394,479, to be able to do WHERE invited LIKE '%,234,%' in your query) you should rebuild your user tables, remove field invited_users and create table like this:

    CREATE TABLE invited_users (
      id INT AUTO_INCREMENT,
      owner_id INT, -- Who's input it is
      target_id INT, -- What's the target user
      PRIMARY KEY ( id),
      UNIQUE ( owner_id, target_id),
      -- Indexes (FOREIGN KEYs!) to users table
    );
    

    And than just select list of users who invited user 234 with query:

    SELECT users.id, users.name
    FROM invited_users
    INNER JOIN users ON invited_users.owner_id = users.id
    GROUP BY users.id
    WHERE invited_users.target_id = 234
    

提交回复
热议问题