Jump to content

MySQL IN function?


kernelgpf

Recommended Posts

$query=mysql_query("select cities.description,cities.name,city_buildings.building_title,city_buildings.building_type,city_buildings.ID from cities,city_buildings where cities.name='$row[location]' and cities.name IN (city_buildings.cityname)")or die(mysql_error());

 

"cityname" is either one city or a list of cities (city1, city2, etc.), but how do I make it work with this query? This query only brings back the buildings with individual city names in "cityname".

Link to comment
https://forums.phpfreaks.com/topic/159199-mysql-in-function/
Share on other sites

where cities.name='$row[location]'

should be

where cities.name IN ($row[location])

 

BUT $row[location] would need to be either 

'cityname'

or

'city1','city2','city3','city4','city5'

 

IF $row[location] is either

cityname

or

city1,city2,city3,city4,city5

you could use

$row['location'] = "'".str_replace(",","','",$row['location'])."'";

to make it fix (this probably isn't the best route but with the data supplied it should work

Link to comment
https://forums.phpfreaks.com/topic/159199-mysql-in-function/#findComment-839589
Share on other sites

This is a very odd query. If you're checking that cities.name is equal to $row['location'], why would you need MySQL IN? I mean you know the value. Just replace the IN part with AND cities.name = city_buildings.cityname and it should work. My point is a value can't be two things at once. So if cities.name is Boston, then no matter what's in the IN clause, you only care for Boston. No need for IN if you only have one thing to look for.

Link to comment
https://forums.phpfreaks.com/topic/159199-mysql-in-function/#findComment-839659
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.