I have a query like this,
select * from my_table where `status`='1' ORDER BY FIELD(city_id,548) ASC;
My purpose is to show all records from the table, but records with city_id=548 should come first.
Currently its showing all records but no desired sorting! Any ideas?
Actually it is working but you need to change it to DESC because FIELD() returns the index of the value in the parameter.
Other than FIELD() which accepts multiple parameters, you can alternatively use = if you only have one condition.
ORDER BY (city_id = 548) DESC
when city_id = 548 is true, it returns 1 otherwise 0 that is why we used DESC.
Try like this
descending order in sql is denoted by DESC keyword I think other values are smaller than 548 that's why it's not work as you expect
select * from my_table where `status`='1' ORDER BY FIELD(city_id,548) DESC;
Related
I have a query where I need to select the latest 25 entries to my database, but inverse the order of the results of that select.
I've come up with:
SELECT indexID, datetime, temperature FROM dataB WHERE userID="4236" ORDER BY indexID DESC, datetime ASC LIMIT 25
But I'm still getting the results in chronological order starting from newest, but I want chronological oldest to newest WITHIN those 25 newest. I'm using PHP's pg_fetch_row() and creating a concatenated string with the results and feeding that into a Morris.js graph, where my data is being graphed backwards because of this query.
How do I reverse the results of a query?
You should try this , first fetch data in descending orders after that use as temporary table and again select in ascending order
SELECT indexID, datetime, temperature FROM (SELECT indexID, datetime, temperature FROM dataB WHERE userID="4236" ORDER BY indexID DESC LIMIT 25) temp order by indexID ASC
Instead of putting the results directly into a string, load them up in an array. When the array is full, use the array_reverse function to reverse the order of the elements in the array. You can now build your string and pass the data to your Morris graph.
Documentation: http://php.net/manual/ro/function.array-reverse.php
Try to add an offset along with a limit to your query. Below example shows an offset which basically gets the total number of rows that match your query and subtracts 25 from it to offset you to start from the "25th last" record which matches your query:
SELECT indexID, datetime, temperature
FROM dataB WHERE userID="4236"
ORDER BY indexID DESC, datetime ASC
LIMIT (( SELECT COUNT(indexID) FROM dataB WHERE userID="4236" )-25), 25;
I was thought how to make a complicated (as I think) SELECT query from table with 3 kind of comments (first - positive comments, second - negative comments and third - neutral comments). Using PHP I would like to SELECT and diplay first negative comments, and right after negative comments diplay all other type of comments. How to diplay them with one SELECT query together with LIMIT that I use to separate for pagination?
Example of table:
id - unique id
type - (value 1-positive, 2-negative, 3-neutral)
text - value
I was thought first SELECT * FROM comments WHERE type='2' ORDER BY id LIMIT 0,100
while(){
...
}
Right after that second
SELECT * FROM commetns WHERE type!='2' ORDER BY id LIMIT 0,100
while(){
...
}
But how use LIMIT for pagination if there is two different SELECT queries?
Use a combination of UNION and LIMIT.
However, you need to determine the bind variables, and specify the number of rows you want to display.
(SELECT * FROM comments WHERE type = '2' LIMIT ?)
UNION
(SELECT * FROM comments WHERE type != '2' LIMIT ?);
SQLFiddle: http://sqlfiddle.com/#!9/6d682/1
Use an IF statement in the ORDER BY clause to change the type 2 to sort first:-
SELECT *
FROM comments
ORDER BY IF(type = 2, 0, type)
LIMIT 1, 20
This will give you all the negative (type 2) comments first, followed by the other comments (whether positive or neutral). You would probably want to add an extra column to the sort just for consistency of display.
I didn't get your case exactly, but I think you may use OR operator to get what you want:
SELECT * from comments WHERE type=2 OR type=-2 ORDER BY type DESC
you can use union to merge group of tables, but you must have the same columns in all the tables, for example:
select commetns,'nagtive' as type from nagtive_tbl limit 10
union
select commetns,'positive' as type from positive_tbl limit 10
union
select commetns,'neutral' as type from neutral_tbl limit 10
this query return table with all the comments from different tables each row contain is type so you can filter it while you building the lists on the client.
I must be missing context, otherwise you would just be fine using:
SELECT * from comments ORDER BY type ASC LIMIT 0,10
So by ordering the type, you'll first get all the items with type 1, followed by 2 and 3.
Just using the limit will chop them in the pieces you want and it will still respect the order.
I have been trying to debug this for quite a while now with no success.
It seems that my mySQL query breaks as soon as I add a where clause to the below query.
Note this returns no results when it should:
SELECT * FROM `workorders` WHERE (`status`='Open') AND
`job_site_name` LIKE '%".$searchword."%' ORDER BY `date_promised` ASC LIMIT 20
This is the same query without the where clause and it returns results as expected, but I need the where status=Open as part of the query...
SELECT * FROM `workorders` WHERE
`job_site_name` LIKE '%".$searchword."%' ORDER BY `date_promised` ASC LIMIT 20
Furthermore, this works (and shows that the status column exists and works in other cases):
SELECT * FROM `workorders` WHERE (`status`='Open') AND `invoice_number` LIKE
'%".$searchword."%' ORDER BY `date_promised` ASC LIMIT 20
I appreciate any assistance in identifying the cause of this problem.
Many thanks in advance!
The issue seems to be both criteria are not met. If you need to use an OR clause then you can do
SELECT *
FROM `workorders`
WHERE (`status`='Open'
OR `job_site_name` LIKE '%".$searchword."%')
ORDER BY `date_promised` ASC
LIMIT 20
I am looking to create an order based on multiple columns in my table - all date related. For example: my columns are: fee_due, fee_2_due, fee_3_due - they all contains results of various dates. However, I want to create an order combining all columns, so fee_due might be 2012-11-03, fee_2_due might be 2012-11-01 and fee_3_due might be 2012-11-02.
My query needs to be:
SELECT *
FROM table_name
ORDER BY [date] DESC
... whereby the dates from the 3 columns join to form one order, regardless of what column they are in.
Hope that makes sense and thanks in advance.
Additionally you can:
SELECT *
FROM table_name
ORDER BY fee_due ASC,fee_2_due DESC,fee_3_due DESC
You can sort each column independently according to your need.
I used the following to make it work:
SELECT * FROM table ORDER BY camp1 ASC, camp2 ASC, camp3 DESC
Have you try this:
SELECT *
FROM table_name
ORDER BY fee_due,fee_2_due,fee_3_due DESC
I currently construct an array of ids and query this array using implode like so:
$sql = "SELECT * FROM item_bank_tb WHERE item_id IN(" . implode(',', $ids) . ")";
the array $ids is constructed in such a way that the ids are in a specific order. However the results of this query are not in that order. I'm guessing as they are all in one query the results appear in the order they were located (ascending).
Is there a way of getting around this? (other than including a field which i can ORDER BY)
many thanks.
Take a look at this example. You have to use field() function.
SELECT * FROM item_bank_tb WHERE item_id IN(1,3,2)
order by field(item_id,1,3,2)
in this way you can get your items in your desired order.
Seriously, what's the problem with adding ORDER BY item_id
If you want the rows of a SQL query returned in a specific order, you must include an ORDER BY in the query.
Use ORDER BY to sort your results. You can either have ORDER BY DESC (descending values from higher to lower) or ORDER BY ASC (ascending from lower to higher).
For example:
SELECT colName,colEmail FROM tblUsers ORDER BY id ASC
Read more here:
http://www.w3schools.com/sql/sql_orderby.asp
you can construct an "orderer" temporay table (if it has not too much data) instead of the id array you can insert to the temp table two values: ID, order
and then join and then query with "order by order"