1

this is my first question on stackoverflow please feel free to edit in proper formate, I want to write a query in mysql that return unique list of all searched ids from a table that contain array of ids.

For example I have a table(event_tbl) which has three columns

  1. id
  2. peopleWhoWillAttend
  3. eventName

Now lets say I have three row of these three value

id  peopleWhoWillAttend  eventName
1   [1]                 'event1' 
2   [1,2,3]             'event2'  
3   [3]                 'event3' 

Now if I search for peopleWhoWillAttend [1,3](this may be longer array not only [1,3]) it should give the result

id  eventName
1   'event1'
2   'event2'
2   'event2'
3   'event3'

Or lets say I have a row of these value

id  peopleWhoWillAttend  eventName
1   [1,2,3]             'event2'  

Now select in such a way that should give the result

id peopleWhoWillAttend  eventName
1          1            'event2'
1          2            'event2'
1          3            'event2'
Sonu Kumar
  • 11
  • 2
  • Welcome to StackOverflow. You should go to the [Help Section](http://stackoverflow.com/help) then read [What types of questions should I avoid asking?](http://stackoverflow.com/help/on-topic) to be sure your question fits the rules and then read [How to Ask a Question on StackOverflow](http://stackoverflow.com/help/how-to-ask) to be able to improve your question accordingly and get the best help. – gp_sflover May 06 '17 at 12:45
  • Keep in mind that "__write/debug-my-code__", "__recommend/search-something-for-me__", "__tutorial__" requests and "__low-effort__", "__unclear__", "__opinion-based__", "**non-programming-related**" questions are [Off-Topic for Stack Overflow](http://stackoverflow.com/help/on-topic). – gp_sflover May 06 '17 at 12:45
  • Show me your database schema with value... – Ahmed Ginani May 06 '17 at 12:50
  • 2
    This question makes me think of the old Supremes song "Stop in the name of love, before you break my heart." https://www.youtube.com/watch?v=9TiLJYH1qFQ You have a **terrible** design for your `peopleWhoWIllAttend` column. **Do not proceed until you create a separate, normalized, table for that one-to-many relationship.** – O. Jones May 06 '17 at 12:58
  • Yes, don't do that. Not only for the select but how will you update the row if more people want to come to event two? – Volt May 06 '17 at 13:05
  • Here is the link that I found for the second part of my question, http://stackoverflow.com/questions/17942508/sql-split-values-to-multiple-rows/page.html#answer-17942691 – Sonu Kumar May 08 '17 at 06:31

1 Answers1

1

Best practice would be to use the power of the database by using unique rows for the peopleWhoWillAttend.

Instead of your rows

Use unique rows for each entry:

id | peopleWhoWillAttend | eventName 
-- | ------------------- | ---------
1  | 1                   | event1
2  | 1                   | event2
3  | 2                   | event2
4  | 3                   | event2
5  | 3                   | event3

The table is then searched by

SELECT id, eventName
FROM *tablename*
WHERE peopleWhoWillAttend='1' OR peopleWhoWillAttend ='3'

Or by using

SELECT id, eventName
FROM *tablename*
WHERE peopleWhoWillAttend IN ('1','3')
Ian S
  • 64
  • 3
  • Hey Ian, thanks for your help, this is a very big project and this is just an example so, I can't change the table structure now because it may needs very good amount of time for that. If you have any solution on the same that will be great. Thanks – Sonu Kumar May 08 '17 at 05:30