search mysql in comma list - php

in my database there is cat_id that is filled by comma list like 2,5,12. but unfortunately when I want execute query like WHERE cat_id='2,5,12' there is no result.
SELECT * FROM `site_content` WHERE `status`!='deleted' AND `cat_id` ='4,6,14'
is there any problem in comma?

This may help..
SELECT * FROM site_content WHERE status!='deleted' AND cat_id IN ('4','6','14')

I have tried your code and it is working.
http://sqlfiddle.com/#!2/790ff/2/0
What is your schema of table?

you can use like
SELECT * FROM site_content WHERE status!='deleted' AND cat_id IN (4,6,14)

$cat = "4,6,14";
$arr = explode(",",$cat);
for($i=0;$i<count($arr);$i++)
{
$query=mysql_query("SELECT * FROM site_content WHERE status!='deleted' AND cat_id ='".$arr[$i]."'");
}

Related

Mysql Order by issue in WHERE IN function

SELECT * FROM (`poll`) WHERE `poll_id` IN ('6,10,5,9,1') AND `poll_status` = '1'
I tried this query. I expected It would give result as I gave. But It sorting the result automatically in ascending. I need result as I give. Kindly give me a solution. Thanks in advance.
You can use FIELD in mysql:
SELECT *
FROM (`poll`)
WHERE `poll_id` IN (6,10,5,9,1)
AND `poll_status` = 1
ORDER BY FIELD(`poll_id`,6,10,5,9,1)
You have to separate your ids like that:
SELECT * FROM (`poll`) WHERE `poll_id` IN (6,10,5,9,1) AND `poll_status` = '1'

I want to select record multiple key with like

I want to select record from table with key.My code is working but it's not select all record regarding Php and Mysql it select only PHP,Mysql record.
My code is
$skill=PHP,Mysql;
mysql_query("select * from tablename(skill) where fieldname(key) like '%$skill%'");
Try to do this
mysql_query("select * from skill where (key like '%PHP%' OR key like '%Mysql%')");
As you have comma separated values in $skill variable like operator will not work correctly, try using following query
select * from skill where FIND_IN_SET(keyn,'$skill')
Demo sql fiddle at http://sqlfiddle.com/#!2/181c74/5
$skill="PHP,Mysql";
mysql_query("select * from skill where key like '%$skill%'");
I am assuming you want to search for records that contain "PHP", "MySQL", or both. What you're doing is searching for the exact string of "PHP,MySQL".
A quick and dirty solution would be to turn $skill into an array that is split on the commas and then perform the search for each term using the OR keyword.
For example:
$skill = array("PHP", "MySQL");
$query = "select * from tablename(skill) where ";
foreach ($skil in $skill)
$query . "fieldname(key) like '%$skil%' OR ";
//code to remove the OR at the very end
Use in into your Query
select * from `skills` WHERE skillslist in ('Php','Javascript')
Run demo Fiddle

need rows with specific column entries in MySQL

I need to get the rows which has a specific column value .
is this is correct for it ??
SELECT *
FROM
tourDB
WHERE
tour_type = insta_deals
in this i want to get all the rows having insta_deals in the column of 'tour_type'.
You are missing " "
SELECT *
FROM
tourDB
WHERE
tour_type = "insta_deals"
^^^ ^^^
SELECT * FROM tourDB WHERE tour_type LIKE 'insta_deals'
Without using ' the value insta_deals is understood by mysql as a column name.
So your need to use ' to specify to mysql that this value is actualy a string.
SELECT *
FROM
tourDB
WHERE
tour_type = 'insta_deals'

Select partial column title in MySQL

I am wondering if there is a way to select part of a column in mysql. For example, I want to select all statistics from my table that end in "_sep_2012". This would give me access to 1_sep_2012, 2_sep_2012, 3_sep_2012 etc...Is there a way to do this?
<?php
$query=mysql_query("SELECT %_sep_2012% FROM statistics WHERE id = '$id'");
$array = mysql_fetch_row($query);
$monthly_total = $array[0];
echo "$monthly_total";
?>
Any assistance would be greatly appreciated. Thanks!
According to your question it should be possible to do something like:
SELECT * FROM statistics WHERE datecolumn LIKE '%_sep_2012'
Not saying it's nice but it should get the data you need. You can add GROUP BY clauses to do a count or sum to get your total like:
SELECT datecolumn, SUM(amountcolumn) FROM statistics WHERE datecolumn LIKE '%_sep_2012' GROUP BY datecolumn
Write this in PHP:
for($i=1;$i<32;$i++)
printf("`%d_sep_2012`, ", $i);
Try this
SELECT * FROM statistics WHERE datecolumn LIKE '%_sep_2012' and id = '$id'

How to find all records which are NOT in this array? (MySql)

I have an array which contains a bunch of ID:s...
I can't figure out how to write a query for finding all records which are NOT inside this array, in mysql.
SELECT * FROM main_table WHERE ..........
Any ideas?
Thanks
Like this:
$str = implode(',', $your_array);
The above statements converts an array into comma-delimited string.
"SELECT * FROM main_table WHERE id NOT IN ('$str')"
More Info:
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
SELECT *
FROM main_table
WHERE id NOT IN(1, 2, 3)

Categories