MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 12th, 2012, 04:02 PM
Coddii Coddii is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 1 Coddii User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 36 sec
Reputation Power: 0
Help needed please - SELECT ALL WHRE id IN (array from another table record)

Hi Smart People,

Here is my puzzle ....

I would like to store an array of comma separated numbers in a user table, and then use this array of numbers to SELECT ALL IN another table.

Example:
Table:USERS
id = [6 ]| favorites = [1,2,4,5,]

Table: FAVORITES
id = [1] | fav = [DOGS]
id = [2] | fav = [CATS]
id = [3] | fav = [FISHS]
id = [4] | fav = [PAROTS]
id = [5] | fav = [MONKEYS]
id = [6] | fav = [HORSES]
id = [7] | fav = [SHEEP]


Desired RESULT =
USER 6 likes DOGS,CATS,PARROTS,MONKEYS


Is this Possible?
Can someone help me with the correct way to write this SQL statement?
Can this be a stored Procedure?

All help gratefully received.

Coddii

Reply With Quote
  #2  
Old September 12th, 2012, 05:04 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 45 m 15 sec
Reputation Power: 1736
First rule, don't store several values in one field.

You should create a USER table, FAVORITES table and a USER_FAVORITES tables.
The last table will contain user_id and favorite_id. If a user has 6 favorites, there will be 6 rows.

In you select, you can use GROUP_CONCAT together with GROUP BY which will then be able to give/display you a comma list for each user, similar to how you define your USER table.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help needed please - SELECT ALL WHRE id IN (array from another table record)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap