Getting data from the end - mysql

I retrieve data from db like that:
select * from MOVIE_COMMENT where USER_ID = 102 LIMIT 10
This gives data to me with ids:
20
21
22
23
24
26
29
30
31
42
What I want is that receiving data from end related user like that:
42
31
30
29
26
24
..
..
..
How can I do that?

select *
from MOVIE_COMMENT
where USER_ID = 102
order by column_name desc
LIMIT 10
use order by?

SELECT * FROM MOVIE_COMMENT
WHERE USER_ID = 102
ORDER BY COLUMN_NAME DESC
LIMIT 10

If you want the last ten rows rather than the first, try sorting data using ORDER BY and DESC.

select * from MOVIE_COMMENT where USER_ID = 102 order by column_name desc limit 10;
This will give you what you want

Related

How can I get prioritize output of a query based on where condition

I have a query which is shown below
SELECT * FROM vehicle_type_master order by standard_volume asc
which yields following result
Vehicle_Type_ID standard_volume
TATA ACE 20
407 30
20 FT 40
So there may arise the following condition, the query looks like
1st Instance:
SELECT * FROM vehicle_type_master where Vehicle_Type_ID = '407'
So for this query the desired result should come like
Vehicle_Type_ID standard_volume
407 30
TATA ACE 20
20 FT 40
2nd Instance:
SELECT * FROM vehicle_type_master where Vehicle_Type_ID = '20 FT'
So for this query the desired result should come like
Vehicle_Type_ID standard_volume
20 FT 40
TATA ACE 20
407 30
So whichever the condition is present in where the condition that should come first and other rows should be displayed as per standard_volume asc.
Any help appreciated
You can use CASE WHEN with multiple ORDER BY
SELECT *
FROM vehicle_type_master
ORDER BY (CASE WHEN Vehicle_Type_ID = '407' then 0 else 1 end) ,
standard_volume asc
SQLfiddle:http://sqlfiddle.com/#!9/1b3907/10
most simple way you can use IF and ORDER BY
SELECT *
FROM vehicle_type_master
ORDER BY IF(Vehicle_Type_ID ='20 FT',0,1),standard_volume asc
sqlfiddle:http://sqlfiddle.com/#!9/1b3907/9

MySQL last 50 records

I have to get the last 50 records from my MySQL database.
Here is the structure of my test database:
ID S1 S2 S3 Date-time Label
13 32 55 33 2017-09-05 13:15:06 temperature
16 111 222 66 2017-09-05 19:22:14 temperature
17 44 55 33 2017-09-05 19:22:14 temperature
18 55 11 88 2017-09-12 14:22:00 temperature
21 77 1 200 2017-09-15 12:24:06 temperature
22 22 55 11 2017-09-19 14:37:00 temperature
How could I show only the last 3 data? for example:
18 55 11 88 2017-09-12 14:22:00 temperature
21 77 1 200 2017-09-15 12:24:06 temperature
22 22 55 11 2017-09-19 14:37:00 temperature
Greetings and thank you.
In Oracle12c you can use the fetch keywork:
SELECT *
FROM table
ORDER BY id DESC
FETCH FIRST 50 ROWS ONLY;
FOR ORACLE:
SELECT * FROM (
SELECT ID,
S1,
S2,
S3,
Date-time,
Label
FROM TABLE
ORDER BY ID DESC)
WHERE ROWNUM <= 50;
FOR MYSQL:
SELECT ID,
S1,
S2,
S3,
Date-time,
Label
FROM TABLE
ORDER BY ID DESC
LIMIT 50;
Here is a quick doc:
https://www.w3schools.com/sql/sql_top.asp
Edit:
For the last 50 rows:
SELECT * FROM (
SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC
Use Top N Query (row num<=50) fro first, for last 50 you can use "order by id desc"
First I was confused with the Post between ORACLE and MYSQL I apologize.
The solution at the end was the following:
SELECT * FROM inv ORDER BY id DESC LIMIT 50
then transform the ARRAY that I collect with the function:
var dorde = d0.reverse ();
thanks for everything.

Have to get the corresponding time stamp when i get max of a column from a table

I need to extract the required fields from a table along with relevant time stamp
SELECT * FROM Glm_Test.LicenseUsage where FeatureId='2';
Output :
VendorId,FeatureId,Total_Lic_Installed,Total_Lic_Used,Reserved,CurrentTime
1 2 106 19 67 2015-12-15 15:00:05
1 2 106 19 67 2015-12-15 15:02:02
1 2 106 19 69 2015-12-15 15:04:02
1 2 106 19 67 2015-12-15 15:06:01
1 2 106 20 67 2015-12-15 15:08:02
select VendorId,FeatureId,Total_Lic_Installed,Max(Total_Lic_Used),Reserved,CurrentTime from Glm_Test.LicenseUsage where FeatureId= '2' group by VendorId,FeatureId;
output:
1 2 106 20 69 2015-12-15 15:00:05
In the above 2 queries
1st query lists all entries from the table
and i want second query to return time stamp for the MAX value of column Total_Lic_Used but somehow it is returning me only timestamp of the first entry.
Help is much appreciated.
Selecting the columns which are not part of an aggregation function like count/max/min/sum... or not in group by clause will give unexpected results:
Other RBBMS wont allow these statements(gives error like):
sql server ==> the select list because it is not contained in either
an aggregate function or the GROUP BY clause
Oracle ==>not a GROUP BY expression
You can do this by a sub query and join
select
a.VendorId,
a.FeatureId,
a.Total_Lic_Installed,
b.max_Total_Lic_Used,
a.Reserved,
a.CurrentTime
from Glm_Test.LicenseUsage a
join (
select
VendorId,
FeatureId,
Max(Total_Lic_Used) max_Total_Lic_Used
from Glm_Test.LicenseUsage
where FeatureId = '2'
group by VendorId, FeatureId
) b
on a.VendorId = b.VendorId and
a.FeatureId = b.FeatureId and
a.Total_Lic_Used = b.max_Total_Lic_Used
sql fiddle demo
You can try this also;
select
`VendorId`,
`FeatureId`,
`Total_Lic_Installed`,
`Total_Lic_Used`,
`Reserved`,
`CurrentTime`
from Glm_Test.LicenseUsage
order by Total_Lic_Used desc
limit 1
demo

SELECT MAX(trans_id),Bank_state,`Forwarded_to` FROM `t_support_details`

Trans_id Bank_State forwarded_to
70 15 1
71 16 2
SELECT MAX(trans_id)
, Bank_state
, Forwarded_to
FROM t_support_details
is giving me result
MAX(trans_id) Bank_state forwarded_to
71 16 1
But Result should be
MAX(trans_id) Bank_state forwarded_to
71 16 2
Please help me
You can try this:
SELECT * FROM t_support_details
ORDER BY trans_id DESC
LIMIT 1
Check this out, Simple and easy
SELECT *
FROM t_support_details
WHERE id=(
SELECT MAX(trans_id) FROM t_support_details
)
As you are looking for the row having maximum trans_id , so its better to use where clause in this particular scenario than the way you are trying to achieve the required output :D.

MySQL Select query - remove unwanted rows

I have a table with the following (I've sorted it by date):
ID prodId date
16 532 2015-08-17
19 535 2014-08-18
18 534 2011-08-17
27 48 2010-08-26
26 1541 2010-08-25
25 1541 2010-08-21
24 1540 2010-08-20
21 48 2010-08-19
20 48 2010-08-18
17 533 2010-08-17
14 532 2010-08-17
22 1540 1970-01-01
I want to select the most recent prodId whos date is in the past. My problem is I am getting multiple prodId with the same value (in this example, 48 and 1541).
My query is currently:
SELECT * FROM `prods` WHERE (date <= '2010-08-26') order by date DESC
How do I change my query to remove the unwanted rows?
SELECT * FROM prods p1
WHERE (date <= '2010-08-26')
AND Date in (Select Max(Date) from prods p2 where p1.prodId = pr.ProdId
and date <= '2010-08-26')
order by activeUntil DESC
add limit 1 to query
Are you looking for the LIMIT statement?
SELECT * FROM `prods` WHERE (date <= '2010-08-26') order by activeUntil DESC LIMIT 1