Jump to content

MySQL SELECT Little issue


cocoscrappy

Recommended Posts

Hello!

 

I have a little problem when trying to select some products from a table.

This is how the table looks like:

412sq.png

 

The query I want to run is SELECT * FROM myTable WHERE category "contains 2 or is 2"

 

I tried LIKE condition (.. WHERE category LIKE '%2%') and it returns only the products whitch has CATEGORY = 2

 

Help please!

Link to comment
Share on other sites

Hi

 

LIKE should work for that (although %2% would also match for 20, 102, etc), but have categories concatenated into a single field like that is a really bad idea.

 

Better to use a second table with multiple rows per product, with one row per product and category.

 

All the best

 

Keith

Link to comment
Share on other sites

Intead, i use a little php code for this:

$cat_wanted = 2; // $_POST[];
$res = mysql_query("SELECT product, category FROM myTable");
while($arr = mysql_fetch_assoc($res)) {

	$arrs = explode(",", $arr['category']);

	if(in_array($cat_wanted, $arrs))
		echo "Product: ".$arr['product']." is in categories: ".$arr['category']."<br />";
}

 

Sorry for double post!  :-[

 

Please post if you find better solutions.

Link to comment
Share on other sites

If they're really just integers separated by commas, you can "cheat" for now with FIND_IN_SET().

 

I can use find_in_set, but when i search for '2', it will find me all the numbers containing 2 (210, 12..)

It is similar to LIKE, i think  :(

No, it won't.

Link to comment
Share on other sites

If they're really just integers separated by commas, you can "cheat" for now with FIND_IN_SET().

 

I can use find_in_set, but when i search for '2', it will find me all the numbers containing 2 (210, 12..)

It is similar to LIKE, i think  :(

No, it won't.

 

Ooops!  :-[ You know you're right

My bad, i tried again and it works both ways! :idea:

Thank you for your support !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.