0

This may be an easy one for some of you, but I am having trouble. I need to do a mysql query that will retrieve data if "$my_id" exists within the querying array. Something like this, or an idea to achieve the same results....

mysql_query("SELECT myArray FROM myTable WHERE my_id IN (myArray) LIMIT 1")

Any ideas?

EDIT:

The array in the table is deliminated by comma's

5
  • 1
    Possible duplicate of stackoverflow.com/questions/907806/… Commented Jun 9, 2012 at 3:58
  • Wait, the array is in the table now? Could you clarify, please? Commented Jun 9, 2012 at 4:00
  • Yeah apparently so. But don't do that. Give us more info on your table structure and we can help better. Commented Jun 9, 2012 at 4:01
  • This is not a duplicate, I have done my research already. The problem is that I need to find the my id within the value I am searching not an array already query'd Commented Jun 9, 2012 at 4:02
  • @ddlshack I have a field in my table row that holds a group of id's that are set up like a deliminated array. I need to search that array, and find out if an id exists in it. If the id does exist, then I want to get that row. Commented Jun 9, 2012 at 4:06

2 Answers 2

1
mysql_query("SELECT myArray FROM myTable WHERE my_id IN ('".implode("', '", array_map('mysql_real_escape_string', $array))."') LIMIT 1");

Edit

I think I see what you're trying to do. You could use mysql LIKE for this, but its really inefficient (it has to do a full table scan). You shouldn't store lists of values in one field in mysql.

You want to create a one-to many relationship between entities. Because you havn't really given us much information, we'll call the entities stored in your table foos. So you have a table named foos that stores your foos. Each foo has a relationship with a number of bars, which are currently being stored in an 'array'. This is called a one-to-many relationship. These bars need to be in a separate table calls bars. You will store one bar per row, and each bar is associated with one foo, so you store foo's id along with each bar. If you wan to find which foos are associated with a certain bar, you just do a simple join.

If bars can be shared between many foos, this is called a many-to-many mapping. Then you need one table for foos, one table for bars, and a third table to map foos to bars. Then you can search which foos are associated with which bars, and which bars are associated with which foos.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, I misinterpreted your question. Don't store a comma-delimited list of values in one mysql field.
Actually, the problem is I need to find a value within the array I am searching for
In your opinion, what is a better alternative of storage?
0

If it's a one dimensional array - just implode it:

implode("','", $array)

IN takes comma separated values. http://www.w3schools.com/sql/sql_in.asp

1 Comment

You misinterpreted my question. I have a field in my table row that holds a group of id's that are set up like a deliminated array. I need to search that array, and find out if an id exists in it. If the id does exist, then I want to get that row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.