I'm using union all clause like
select * from products where id=1
union select * from products order by price desc
But as result i got the product with id 1 at the middle of the list as it makes a filter with price
How can i get the product with id 1 at the first row?
You seem to want a conditional sort rather than union:
select *
from products
order by case when id = 1 then 1 else 2 end, price desc
This puts first the row with id 1, followed by the rest of the records ordered by decreasing price.
Related
I have an sql table with products and price and I want to get 2 products with the highest price and 2 with the lowest price.
SELECT title, price
FROM products
GROUP BY price
ORDER BY price ASC limit 2
I tried this to get the lowest 2 values but how do I get the highest 2 in the same query?
Use a union of two limit queries:
(SELECT title, price FROM products ORDER BY price LIMIT 2)
UNION ALL
(SELECT title, price FROM products ORDER BY price DESC LIMIT 2)
The subqueries on the top and bottom will return the records corresponding to the 2 lowest and 2 highest prices, respectively.
Is there any way how to order top rows in a query in a different way than the rest? For example, if I have a list of products with name and price, I would like to get the list ordered in a way that 10 most expensive products are on top ordered by price desc, and the rest below is ordered by product name.
What comes in my mind is something like this:
SELECT id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
ORDER BY name
but this query does not execute, it prints Incorrect usage of UNION and ORDER BY, furthermore if I wrap the selects into another selects, it prints LIMIT & IN/ALL/ANY/SOME subquery. Any idea?
select * from
(
SELECT 1 a, id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT 0, id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
)
order by a*price desc, name
Hi I am trying to select row in mysql whose visitor id=1 and max id then it should be the last row as I focus in this picture
but it is showing something else, it showing this output
here the mysql code I tried
SELECT *,max(id) FROM activity WHERE visitorid=1
you can do it with:
SELECT * FROM activity WHERE visitorid = 1 ORDER BY id DESC LIMIT 1
this will order your rows (where visitorid = 1) descending by id and select only the first one.
When there exists multiple entries for an id in a table
and you still want to fetch the latest, then
filter the records in descending order of auto incremented primary key and limit to top record.
Example:
select * from activity
where visitorid = 1
order by id desc
limit 1
I have an SQL query that returns a list of results. The sql is :
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory
The output is something like
----------------
category|qty
----------------
a | 10
b | 100
c | 554
Based on the result of the sql statement above, how do I retrieve the row with maximum quantity? (in this case I want the query to return just "c" )
you can add order by + limit 1 to get this :
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory
order by qty desc limit 1
In order to fetch the highest qty, just get the first result ordered by qty like:
SELECT * FROM items ORDER BY qty desc limit 1
In your own case,
SELECT fcategory,sum(fquantity*fprice) AS qty FROM items GROUP BY fcategory ORDER BY qty LIMIT 1
I have table with folowing structure. The content in this table is duplicate. I want to show the record with min price.
GROUP IDX NAME PRICE
141003 6 0285-00 499.88000
141003 2 028500 519.13000
141003 1 0285-00 424.12000
141005 2 02851 559.13000
141005 1 0285-1 434.12000
RESULT TABLE
141003 1 0285-00 424.12000
141005 1 0285-1 434.12000
I trying this but returned result is:
141003 6 0285-00 499.88000
Query:
select group, idx, name, price
FROM table
GROUP BY group
ORDER BY price;
Use the following query if you want to know the minimum price in each group:
select `group`, `idx`, `name`, MIN(price) AS min_price
FROM `table`
GROUP BY `group`;
Documentation for 'MIN' function
SELECT group, idx, name, price
FROM table
WHERE group = 141003
ORDER BY price ASC
LIMIT 1;
Would do the trick.
First of all group is a reserved word in MySQL. You should use ` around it or rename the table. Like this:
select `group`, idx, name, price from test_2 where `group` = 141003 order by price desc limit 1;
Why do you want to do group by if the group is in the the where condition with one value? Do you want the cheapest item from all the groups?