I have a table with the following information:
ID Name Value
=== ===== =======
1 apple 5
2 green 10
3 orange 1
4 blue 0
5 fish 3
6 lettuce 2
7 cabbage 4
8 computer 1
9 car 0
10 sport 9
11 racing 15
I want to be able to only pull 3 highest value records in this table. So for example i would want to pull the following in that order.
11 racing 15
2 green 10
10 sport 9
I know i can use ORDER BY to order them by value so it gives me the highest first. But how would i query to only get those records?
You can do as
select * from your_table order by Value desc limit 3
In SQL Server
SELECT TOP 3 * FROM tablename order by Value DESC
You can ORDER BY multiple columns. If I'm interpreting your request, you could ORDER BY VALUE DESC, NAME. So the result with the 10 would still be first, followed by all those with 0, ordered alphabetically by name.
Related
I have a table that looks like this
id
1
2
4
5
6
10
11
So a bunch of consecutive values, an unknown number of absent fields and then other consecutive values.
What I am trying to achieve is to get
id
stint
1
0
2
0
4
1
5
1
6
1
10
2
11
2
By incrementing every time the number of the stint, which I can later use for summing over other columns.
Is it possible? Thanks
If your MySQL version support window function.
You can try to use LAG window function in subquery to get previous id column, then use SUM condition aggregate window function.
Query #1
SELECT Id,
SUM(id - n_Id > 1) OVER(ORDER BY id) stint
FROM (
SELECT *,LAG(id,1,id) OVER(ORDER BY id) n_Id
FROM T
) t1
Id
stint
1
0
2
0
4
1
5
1
6
1
10
2
11
2
View on DB Fiddle
As the result of the query, I want to get all rows (Drivers), order by the drivers who got most series wins.
If a driver has won 4 tacks at least one or more times but failed to win the remaining track at least once, his series count is 0.
Driver Table
ID|Name| .........
1 A
2 B
3 C
4 D
Tracks Table
TID |FK|Track1_Wins|Track2_Wins| Track3_Wins|Track4_Wins|Track5_Wins|
1 1 5 6 3 2 4
2 2 2 4 0 5 3
3 3 6 3 9 4 7
4 4 5 8 2 4 1
My code sample
SELECT `Drivers`.`Name`, LEAST(`Track1_Wins`, `Track2_Wins`, `Track3_Wins`, `Track4_Wins`, `TRACK5_Wins`) AS Series
FROM `Drivers`, `Tracks`
ORDER BY Series DESC;
Accidently I got part expected output when I use WHERE with Driver ID
SELECT `Drivers`.`Name`, LEAST(`Track1_Wins`, `Track2_Wins`, `Track3_Wins`, `Track4_Wins`, `TRACK5_Wins`) AS Series FROM `Drivers`, `Tracks` WHERE `Drivers`.`ID` = 2 ORDER BY Series DESC;
It will give the expected result but with Same Driver Name as expected
B 3
B 2
B 1
B 0
My expected output is
Name | Series
C 3
A 2
D 1
B 0
Run this,
SELECT d.`Name`,
LEAST(`Track1_Wins`, `Track2_Wins`, `Track3_Wins`, `Track4_Wins`, `TRACK5_Wins`) AS Series
FROM `Drivers` d INNER JOIN `Tracks` t
ON t.`FK` = d.`ID`
ORDER BY Series DESC;
This returns the user name associated with the FK. Also, try to use kebab_case and lower case for all your column and table name. Makes it much easier to run the code
In MySQL, I am trying to sum values in a column given certain conditions. I have an example of some data below
Team Season Mth Stat
A 1 1 4
A 1 1 4
A 1 2 7
A 1 2 9
B 1 1 6
B 1 1 6
B 1 2 6
B 1 2 9
C 1 1 1
C 1 1 3
C 1 2 3
C 1 2 6
But I need the output to show up as
Team Season Mth Stat
A 1 1 8
A 1 2 16
B 1 1 12
B 1 2 15
C 1 1 4
C 1 2 9
So the Stat column is now the sum of the cells such that Match, Season, and Team are all the same. I have the code below. I see a lot of answers that use 'case' but that seems to be given logical operators that are not equal to each other. When I do it below, now it doesn't recognise the table where the columns are coming from. I do have a inner joins but the data itself is from one table. I get another error as well on the sum function because it requires one argument.
select
Team
,Season
,Match
--this is where I get lost-----------
sum(
select
Stat
From
table
Where
Mth=Mth
AND Season=Season
AND Team=Team
)
--end of getting lost----------------
FROM
table
Where
Season IN (1,2)
GROUP BY
Team
,Season
,Mth
Order BY
Team ASC
Edit:
It turns out I need to use GROUP BY as the comments suggest. So I am not summing within a table, but I sum the variable given the Group By parameters.
Unless I'm missing something, it's simply:
SELECT Team
,Season
,Match
,Sum(Stat)
FROM table
GROUP BY
Team
,Season
,Match
It's simple as this:
SELECT Team,
Season,
Match,
SUM(Stat)
FROM Table
WHERE Season IN (1,2)
GROUP BY Team,
Season,
Match
ORDER BY Team ASC
Please look at the SQL Fiddle example.
Suppose I have a table like so,
unqiue_data int(10),
not_unique_data int (10)
unique_data not_unique_data
1 1
2 1
3 2
4 2
5 2
select * from some_table order by not_unique_data DESC;
What I need to do, is randomize this SELECT query, but in a very two particular ways that I just can't figure out how to do. Firstly, I want unique_data randomized, so that the SELECT query could return something like (randomly):
unique_data not_unique_data
2 1
1 1
4 2
3 2
5 2
The second requirement I have is that, unique_data appears multiple times, but in a very specific order.
In an ideal world, I need is so that it could return something like
unique_data not_unique_data
4 2
3 2
5 2
1 1
2 1
3 2
5 2
4 2
2 1
1 1
5 2
4 2
3 2
What I mean by this is, I need it so that each unique_data (4,3,5), (3,5,4), (5,4,3) The first number of each set appears only once while still being ordered by not_unique_data.
How to do this?
Well for this problem you have to make sure that 100 products related to a product
how many of them have appeared for that product
how many of them will be appeared for that product
We can use a temporary table to do so
SELECT unique_data, not_unique_data, 0
INTO temp_newtable
FROM some_table
ORDER BY RAND()
Now we will get a randomly organized table and by default seen=0 (seen to know it has been appeared for that product or not)
unique_data not_unique_data seen
4 2 1
3 2 1
5 2 0
1 1 0
2 1 0
3 2 1
So whenever some product related to product appear on page you need to update seen column to 1, when you are out of this table truncate and generate random data for usage again
I think you are looking for this https://stackoverflow.com/a/3990479/2552551
SELECT * FROM
(
SELECT * FROM some_table
ORDER BY not_unique_data DESC
LIMIT 100
) T1
ORDER BY RAND()
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.