1

Hi guys I have this situation

tabperfil
ta

bperfil.Cidade_Id -> values = 12,34,645,21

then I have a $id = 12

so I'm try to do this query

select * from tabperfil limit 0,5

but with a condiction for get the values of Cidade_Id and check with $id

if was a reverse situation I know

select * from tabperfil WHERE Cidade_Id IN ($id)

so how do something to works like this

select * from tabperfil WHERE ($id) in Cidade_Id
blake305
  • 2,196
  • 3
  • 23
  • 52
Alvaro Louzada
  • 433
  • 1
  • 6
  • 23
  • If you can't use proper grammar, why should we answer your question? – blake305 Jan 12 '12 at 18:26
  • Have a look at mysql regular expression... you're going to be looking for scenarios where id matches the string up to the first comma, immediately follows a comma and ends with a comma, or where it follows a comma and ends the line. – Ben D Jan 12 '12 at 18:27
  • nobody is perfect my friend, i m not a english expert but i try... if u can use, congrat's . i m learning – Alvaro Louzada Jan 12 '12 at 18:28
  • @blake305 There are a lot of non-native English speaking individuals here. Requiring proper grammar before answering a question would eliminate a lot of the help that could be provided. – jprofitt Jan 12 '12 at 18:28
  • I'm sorry i dont get it, can you please explain it more. What do you actually want, what is the code yo ugot, what is the problem – Henk Jansen Jan 12 '12 at 18:28
  • @blake305 - I don't think that's fair. Not everyone is a native English speaker. – James Allardice Jan 12 '12 at 18:29
  • @jprofitt It was almost unreadable before I edited it. – blake305 Jan 12 '12 at 18:29
  • @JamesAllardice It was almost unreadable before I edited it. – blake305 Jan 12 '12 at 18:30
  • So what is the difference you expect Cidade_Id IN ($id) and ($id) in Cidade_Id . please explain why you trying the second one!!!!! – zod Jan 12 '12 at 18:30
  • Possible duplicate of [mysql check if numbers are in a comma separated list](http://stackoverflow.com/questions/2674011/mysql-check-if-numbers-are-in-a-comma-separated-list) – Matteo B. Jan 12 '12 at 18:30
  • @blake305 I was the last to approve your edit, so I saw it before it went through. – jprofitt Jan 12 '12 at 18:30
  • 1
    @blake305 - Your edits consisted of a few capitalisations and a bit of missing code formatting. I would hardly say it was "unreadable". As the OP said in his comment, at least he's learning. These comments will likely get removed if we carry on like this, so let's leave it at that. – James Allardice Jan 12 '12 at 18:31
  • @zod my english like blake305 says not good, so maybe you can understand. i have a table citys there is a array (1,23,45) when people go to the website they select a city and i set the cookie to the city id like 1 - then i want to select on db the results only if is in table city... did u get? – Alvaro Louzada Jan 12 '12 at 18:44

1 Answers1

5

WHERE FIND_IN_SET($id, Cidade_Id) > 0

But you should really normalize your database. Having comma-seperated values in 1 column is asking for trouble.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • 1
    I really think that everyone who works with databases, or at least the programming of database applications, should know how to normalize a database, and not only know it, but also do it. – Henk Jansen Jan 12 '12 at 18:40
  • ok, i did this - maybe not right away but works citys => (1,3,5,7) $id = 1 $sql "SELECT * FROM TABLE WHERE (citys LIKE '$id,%' or citys LIKE '%,$id,%' OR citys LIKE '%,$id')"; thanks – Alvaro Louzada Jan 12 '12 at 19:46
  • 1
    Don't do that. Change your database layout so you don't have a column with comma-separated values anymore if possible. – Wrikken Jan 12 '12 at 19:52