Jump to content


Photo

Proper syntax for WHERE in query...


  • Please log in to reply
3 replies to this topic

#1 CorkyMcDoogle

CorkyMcDoogle
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 August 2006 - 05:35 PM

Hello...

I'm trying to set up a way to sort database results by the type of article it is. I have links Show: Type1 - Type2 - Type3 - All. The single type links work correctly but when I want to show all of them together nothing comes back. It's because the syntax I'm using is not correct I'm guessing.

Whats the correct way to ask for multiple types in the WHERE?

switch ($_GET['type']){
	case 'type1':
	$type = "type1";
	break;
	case 'type2':
	$type = "type2";
	break;
	case 'type3':
	$type = "type3";
	break;
	case 'typ4':
	$type = "type4";
	break;
	case 'all':
	$type = "type1,type2,type3,type4";
	break;
		}
}
else {
$type = "type1";
}


$query = "SELECT DISTINCT type, title, itemtext, DATE_FORMAT(postdate,'%b %D, %Y - %l:%i %p') AS postdate FROM item WHERE type = ('$type') ORDER BY rand() LIMIT 10";


#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 09 August 2006 - 05:39 PM

SELECT * FROM table WHERE fieldname = 'text';

or

SELECT * FROM table WHERE fieldname LIKE '%text%';

Hope this helps
Tell me the problem, I will try tell you the solution

#3 CorkyMcDoogle

CorkyMcDoogle
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 August 2006 - 06:36 PM

Yea but I'm trying to get multiple types into one WHERE.

If type is set to 'all' I want it to show all types. Something like...
WHERE type = 'type1 OR type2 OR type3 OR type4'

How can I make what I want fit into the $type variable?

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 09 August 2006 - 06:53 PM

WHERE type = 'type1' OR type = 'type2' OR type = 'type3' etc.
or
WHERE type IN ('type1','type2','type3','type4')





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users