How can I ORDER BY a variable? - php

I don't know why it's not working... I'm trying to "order by" a created variable like this:
mysql_query("SELECT *,DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') as time FROM ab_list WHERE time = '24/08/2013' ORDER BY banfrom DESC LIMIT 0,50");
Or,
$hr = date('d/m/Y');
mysql_query("SELECT *,DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') as time FROM ab_list WHERE time = '$hr' ORDER BY banfrom DESC LIMIT 0,50");

Assuming the variable $hr is in the format %d/%m/%Y, you can use having instead of where to filter the records.
mysql_query("SELECT *,DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') as time
FROM ab_list
HAVING time = '$hr'
ORDER BY banfrom DESC
LIMIT 0,50");
The reason to use HAVING and not WHERE in this case is because time is computed column.

It's because you are using an ALIAS in the WHERE clause that was created on the same level of the SELECT clause. There are two ways to make it working,
One, using the calculated column directly in the WHERE clause
WHERE DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') = '24/08/2013'
Second, using a subquery
SELECT a.*
FROM (put your whole query here) a
WHERE a.time = '24/08/2013'
WHERE cannot use ALIAS because in the SQL Order of Operation the WHERE clause is executed before the SELECT clause. Here's the list of order of operations:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause (Alias is created here)
ORDER BY clause

Related

GROUPBy AND ORDER BY not working together

I have records like this
here is my query
SELECT `Chat`.`id`,
`Chat`.`sender_id`,
`Chat`.`receiver_id`,
`Chat`.`message`,
`Chat`.`datetime`,
`Chat`.`converstation_id`
FROM `gopher`.`chat` AS `Chat`
WHERE ((`Chat`.`sender_id` = 10)
OR (`Chat`.`receiver_id` = 10))
GROUP BY converstation_id
ORDER BY `Chat`.`id` DESC
But here order now is not working and this is the result I am getting after running this above query
You have not used any aggregate function , so your group by is just returning 1st data set. There are several ways to fix it
Remove group by and just use order by if we you want to sort by conversation_id
Use aggregate function
SELECT `Chat`.`id`,
`Chat`.`sender_id`,
`Chat`.`receiver_id`,
`Chat`.`message`,
`Chat`.`datetime`,
`Chat`.`converstation_id`
FROM `gopher`.`chat` AS `Chat`
WHERE ((`Chat`.`sender_id` = 10) OR (`Chat`.`receiver_id` = 10))
GROUP BY converstation_id
ORDER BY `Chat`.`id` DESC LIMIT 0,1

select with group by query and order by query in mysql

hi i have one database and i use this query for get latest records using group by and and order by
i have use this code for get all record
SELECT id,time FROM eventlog WHERE event = '2' ORDER BY `eventlog`.`id` DESC
and i get this output:
i want to get latest time of every group
i have use this query for group by with order by query
SELECT id,time FROM eventlog WHERE event = '2' GROUP BY id ORDER BY time DESC
Try this:
SELECT id,MAX(`time`) as `time` FROM eventlog WHERE event = '2' GROUP BY id DESC

Order by: Difference between two columns

i have a question about a sql query. I have two columns in table users:
Moneytotal and Moneythisweek
Right now i would like to order the results on difference.
How bigger the difference between Moneytotal and Moneythisweek the higher on the ranking.
Normally i use:
$lsel_rank = mysql_query("select * from users ORDER BY Moneytotal DESC");
$rank = mysql_fetch_array($lsel_rank);
But now i want to do something like:
lsel_rank = mysql_query("select * from users ORDER BY Difference between Moneytotal
AND Moneythisweek DESC");
Can anyone help me?
SELECT * FROM `users` ORDER BY Moneytotal - Moneythisweek DESC
Just simply use this
$lsel_rank = mysql_query("select *,'moneytotal - monethisweek' as difference from users ORDER BY difference DESC");
Option 1: (Doing calculation in orderby)
SELECT * FROM `users` ORDER BY (Moneytotal - Moneythisweek) DESC;
Option 2: (Using numbered index in order by and calculating the difference and make available in the select query's resultset
SELECT (Moneytotal - Moneythisweek) difference, a.* FROM `users` a ORDER BY 1 DESC;

Search MySQL database, order results by COUNT

I have a database table 'sales_list'. In this is rows of sales records attributed to a users_sales_guild_id. I'd like to query the table and order results by the number of sales made by each user, highest to lowest.
I thought this query would do it, but alas no...
$total_query = "SELECT *, COUNT(users_sales_guild_id) AS users_sales_guild_id_count FROM sales_list WHERE sales_entry_date BETWEEN '2013-10-01 00:00:00' AND '2014-11-30 23:59:59' ORDER BY users_sales_guild_id_count DESC";
$total_rs = mysql_query($total_query) or trigger_error ("Query: $total_query\n<br>MySQL Error: " .#mysql_error()); // Run the query.
$num_rs = mysql_num_rows($total_rs);
This query returns 1 record. rather than a selection of records ordered by the number of sales by each user.
Your assistance is much welcomed.
count(*) will return one row unless there is a group by clause, so the query should be as
SELECT *,
COUNT(*) AS users_sales_guild_id_count
FROM sales_list
WHERE sales_entry_date BETWEEN '2013-10-01 00:00:00' AND '2014-11-30 23:59:59'
group by users_sales_guild_id
ORDER BY users_sales_guild_id_count DESC
UPDATE : Its better to select col1,col2 ..... instead of * while doing group by - Point raised by InoSHeo
check this link
http://sqlfiddle.com/#!2/1201d/6
check this link if you would like to get details based on username http://sqlfiddle.com/#!2/1201d/4 here i have used type instead you can use username

ORDER BY RAND() returns duplicates

I tried using distinct as well and it returns duplicates.
$cubes = mysql_query("SELECT distinct * FROM posts ORDER BY RAND() $limit ") or die(mysql_error());
I just want to take my posts table... and return it in a random order without duplicates.
Select only the distinct id's you need, e.g.
SELECT distinct id FROM posts ORDER BY RAND() $limit
Distinct works over 'all' rows you select, so if you (for example) have a unique timestamp field, chances are you'll return every single row.
Are you sure that you want to execute a SELECT DISTINCT * FROM ... and not just a SELECT DISTINCT column_name FROM ... ?
See the SQL DISTINCT STATEMENT doc for more infos.

Categories