I have a table:
id
product_id
price
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
6
6
6
and I have a data set :
[{id:2:weight:8},{id:5,weight:6},{id:4,weight:3}]
I want to get the products which price < 5 and sort them first by weight asc from the dataset above and then sort them by price desc, so the result should be:
[{
id:4,product_id:4,id:4
},{
id:2,product_id:2,id:2
},{
id:3,product_id:3,id:3
},{
id:1:product_id:1,id:1
}]
You can use CASE syntax to get the desired results.
SELECT * FROM (
SELECT t.id, t.product_id, t.price,
CASE
WHEN t.id=2 THEN 8
WHEN t.id=5 THEN 6
WHEN t.id=4 THEN 3
ELSE 0
END AS weight
FROM mytable t
WHERE t.price < 5
) res ORDER BY res.weight ASC, res.price DESC;
you can specify the way to short for each column declared in the sorted list. here is an example:
select *
from myDataset
where price < 5
order by price desc, weight asc
Related
I have been given a leaderboard, I need to return the row correspond to 4th to 8th rank in a leaderboard?
id name score
1 gongy 3001
2 urandom 2401
3 eduardische 2477
4 Gassa 2999
5 bcc32 2658
6 Alex_2oo8 6000
7 mirosuaf 2479
8 Sparik 2399
9 thomas_holmes 2478
The query I have is not being accepted
select b.name from
(
select a.name as name ,a.score as score from
(
select name,score from leaderboard order by score desc limit 8
)a
order by a.score
limit 5
) b
order by b.score desc;
You can use LIMIT OFFSET
select id,
name,
score
from leaderboard
order by score desc
limit 4 offset 3 ; --- fetch 4 records, begin with record 4 (OFFSET 3)
https://dbfiddle.uk/Ys-3jC4L
Above query skips the first 4 rows and limit the result to 4 make it from 4th to 8th
select x.id,x.name,x.score
from
(
select id,name,score,
row_number()over(order by score desc)xcol
from your_table
)x where x.xcol >=3 and x.xcol<=8
May be something like this ?
plz refer the table to provide the sql query to get result like that.
id value
1 10
2 15
3 30
4 10
5 11
6 12
Desired output:
id value
1 55
2 33
http://sqlfiddle.com/#!9/21cbc8
Divide id by 3, round it up, group it (with a sum):
SELECT
ceiling(id / 3) AS NewID,
sum(Value) AS SumValue
FROM MyTable
GROUP BY ceiling(id / 3)
With a variable:
SET #GroupVar = 3; -- Set this number to whatever you want to group by
SELECT
ceiling(id / #GroupVar) AS NewID,
sum(Value) AS SumValue
FROM MyTable
GROUP BY ceiling(id / #GroupVar);
I have a table:
Numbers
id type_id
1 1
2 1
3 2
4 1
5 2
6 2
7 1
8 1
9 2
etc...
I need to get 3 random records of type 1 and the same number of random records of type 2. How can I get it with one query?
(select * from your_table where type_id = 1 order by rand() limit 3)
union all
(select * from your_table where type_id = 2 order by rand() limit 3)
Using MySQL
SELECT <<columns>> FROM <<table_name>>
ORDER BY RAND()
LIMIT <<count>>
Using Oracle
SELECT * FROM (SELECT * FROM <<table_name>> ORDER BY
SYS.DBMS_RANDOM.VALUE) WHERE ROWNUM <<Count>>
I am using MySQL as a database.
Now I got everything working correctly but as my client wants to have filter on the website I am in the some sort of problem of what exactly I need to do and what would be the best way of doing it.
So this is my data ( I will simplify it as much as possible )
id name price nr.bed nr.bath
---------------------------------
1 a 33 2 4
2 b 100 5 1
3 c 102 2 2
4 d 85 1 1
5 e 37 6 4
6 f 19 2 1
So first time page loads I am using this query to get first 5 from the database:
SELECT * FROM hotel LIMIT 5
And I get this:
id name price nr.bed nr.bath
---------------------------------
1 a 33 2 4
2 b 100 5 1
3 c 102 2 2
4 d 85 1 1
5 e 37 6 4
After that each time I am calling this:
SELECT * FROM hotel WHERE id>'last_id(in this case 5)' LIMIT 5
And I am getting:
id name price nr.bed nr.bath
---------------------------------
6 f 19 2 1
And so on...
But now I need to use filter for example I will have filter for price
So I need to have something like:
SELECT * FROM hotel where id>5 order by price desc LIMIT 5
But then I am loosing my id order and I can't get next five from the database because I can't compare to and id of hotel because everything is orderd by price.
How can I achieve this?
Do I need to add another column or something which will keep my order as it is? Everything is presented on the website using and id from the table.
EDIT:
I am not sure if that is even possible because I need to have id ordered in asc and price in desc but I am not sure if we can combine those two together without adding another column or something.
EDIT2:
I would like to get something like this
id id_copy name price nr.bed nr.bath
-----------------------------------------
1 3 c 102 2 2
2 2 b 100 5 1
3 4 d 85 1 1
4 5 e 37 6 4
5 1 a 33 2 4
6 6 f 19 2 1
First, you should never use limit without order by when the ordering is important. Your queries should be like:
SELECT *
FROM hotel
WHERE id>'last_id(in this case 5)'
ORDER BY id
LIMIT 5
For your question, you want a subquery:
select *
from (SELECT * FROM hotel where id>5 order by price desc LIMIT 5
) t
order by id
The inner query selects the 5 by price. The outer one orders by id.
If you want to use this for "pagination", then you should become familiar with the offset argument to limit. This is described in the MySQL documentation here.
Order two columns then:
SELECT * FROM hotel where id>5 order by price desc , id asc LIMIT 5
i think this what you are looking for
SELECT * FROM hotel order by price desc limit 0 ,5
then if you want next 5
SELECT * FROM hotel order by price desc limit 5 ,5
then
SELECT * FROM hotel order by price desc limit 10 ,5
and so on...
you can make this in php like that
$sql="SELECT * FROM hotel order by price desc "
if (sombutton is clicked) {
$sql .= " limit 0,5" ;}
if (sombutton is clicked){
$sql .= " limit 5,5" ; }
or you can do it by a loop with variable $i everytime you add 5.
I have a schema like the following
id (INT)
Cycle_Number (INT)
Cycle_Day (INT)
Date (date)
...other columns irrelevant to the question...
How can I get the row that has the max Cycle_Day within the max Cycle_Number
For example, say I have the following data
ID Cycle_Number Cycle_Day Date
1 1 1 2011-12-01
2 1 2 2011-12-02
3 2 1 2011-12-03
4 2 2 2011-12-04
5 2 3 2011-12-05
6 2 4 2011-12-06
7 3 1 2011-12-07
8 3 2 2011-12-08
9 3 3 2011-12-09
The query would return row 9. (It has the highest Cycle_Day within the highest Cycle_Number)
Thanks
this one is compatible MySql 5.5 with no joint tables
SELECT id
FROM cycles
ORDER BY Cycle_Number DESC , Cycle_Day DESC
LIMIT 0 , 1
Regards
This SQL query should provide the max value you want.
SELECT ID, Cycle_Number, Cycle_Day, Date
FROM yourTable AS t
CROSS JOIN (
SELECT MAX(Cycle_Number) AS Cycle_Number FROM yourTable
) AS sq USING (Cycle_Number)
ORDER BY Cycle_Day DESC LIMIT 1