Mysql Order by issue in WHERE IN function - php

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'

Related

PHP Query and ORDER BY

I got this code and need to sort the output by name:
$output = array();
while($res = mysql_fetch_assoc($res_handle))
{
$output[$res['_name']][] = $res['modell'];
}
Could anyone help me ? Thanks.
can you please try and let me know if this helps
ksort($output);
You can sort the table using several functions that php provide
or you can modify your sql query add the ORDER BY name
Use ORDER BY name in select query as follow :
SELECT * FROM Table_Name ORDER BY name
Thanks...
best way is use sorting in query, for example:
SELECT _name, modell FROM table_name ORDER BY _name ASC, modell ASC
This sort everything from A to Z. If you need results to sort from Z to A use DESC.
To sort the results in the field can be used, for example:
asort($output);
Overview table as sort fields:
http://php.net/manual/en/array.sorting.php
Simply you can use ORDER BY in Query
SELECT * FROM table_name ORDER BY column_name;
OR
Use asort in PHP array
asort($output[$res['_name']]);
Example
<?php
$name_array = array("0"=>"cc", "1"=>"aa", "Joe"=>"bb");
asort($name_array);
print_r($name_array);
?>
Result
Array ( [1] => aa [Joe] => bb [0] => cc )
Refer http://www.w3schools.com/php/php_arrays_sort.asp

mysql code Is there any method to solve this ? substr field column

Is there any method to solve this problem ?
in mysql column field date is value as timestamp and i want to fetch all row date which has specific value as format Y-mm ( 2016-05 )?
please help me i am stacking in this problem, Is it possible ?
select * from article where postby = 'admin' AND substr(`date`,0,7) = '2016-05'
Try 'date_format()' mysql function:
select * from article where postby = 'admin' AND date_format(`date`,'%Y-%m') = '2016-05'
try to CAST it first, if you have to use SUBSTR
SELECT * FROM article WHERE postby = 'admin'
AND SUBSTR(CAST(`date` AS CHAR(10)),0,7) = '2016-05'

search mysql in comma list

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]."'");
}

Did I mess up my where clause? Getting unexpected results

$active_sth = $dbh->prepare("SELECT * FROM user_campaign
WHERE status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
AND uid=:uid
ORDER BY status ASC");
$active_sth->bindParam(':uid', $_SESSION['uid']['id']);
$active_sth->execute();
I am positive $_SESSION['uid']['id'] = 7
but it will also pull results of id 10 or any other number.
Is my AND/OR clause written wrong?
Yes, query is wrong
SELECT * FROM user_campaign
WHERE (
status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
)
AND uid=:uid
ORDER BY status ASC
You have to group all ORs to make sure that row got one of this values, and separately check if it have given uid.
The proper way to write that is:
SELECT * FROM user_campaign
WHERE status IN ('blasting', 'ready', 'followup_hold', 'initial_hold')
AND uid =: uid
ORDER BY status ASC
You should use IN instead of that huge amount of ORs :)

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'

Categories