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.
Related
I have an sql table that saves Locations,
ID Name X Y Z
-------------------------
1 Loc1 12 24 45
2 Loc2 12 24 60
3 Loc3 54 32 33
4 Loc4 54 32 64
5 Loc5 98 66 90
6 Loc6 98 66 77
7 Loc7 44 50 98
Some Location coordinates (x and y) are duplicated by mistake
I want to select and delete one of them.
ID Name X Y Z
-------------------------
1 Loc1 12 24 45
2 Loc3 54 32 33
3 Loc5 98 66 90
4 Loc7 44 50 98
Is this possible with sql query?
I think you can use the alter-table command and just drop the whole column.
ALTER TABLE table_name
DROP COLUMN column_name
with cte
as
(
select *,row_number() over (partition by x,y order by x) as rn
)
delete from cte where rn>1
As seen pointed out in comments,this deletes a random row (undeterministic),to make it deterministic order by name in your case
For MySQL you can use:
DELETE mytable
FROM mytable
JOIN (SELECT MIN(ID) AS ID, X, Y
FROM mytable
GROUP BY X, Y
) AS t ON mytable.X = t.X AND mytable.Y = t.Y AND mytable.ID > t.ID
Demo here
Its easy:
Add an INDEX over the 2 Columns with IGNORE:
ALTER IGNORE TABLE YOUR_TABLE
ADD KEY (X,Y);
Thats all
I have the following structure
Country - UserId - Points
840 23 24
840 32 31
840 22 38
840 15 35
840 10 20
250 15 33
724 17 12
etc
I want to get the position of user in the ranking of each country accordin points
I'm using
select #rownum:=#rownum+1 Num, Country, UserId, points
from users , (SELECT #rownum:=0) r
where country=840 order by points DESC ;
I want to get the position of a single user inside his country
In this example, in country 840, if I select user id=23, I'll get position 4
Country - UserId - Points- Order
840 22 38 1
840 15 35 2
840 32 31 3
840 23 24 4
840 10 20 5
Try doing:
select * from (
select #rownum: = #rownum + 1 Num,
Country,
UserId,
points
from users, (select #rownum: = 0) r
where country = 840
order by points desc
) a
where userId = 23
Using your query you'll receive row number in your results so it's not what you want to. Best way is to generate positions and save them to separated column. This way you'll be able to select it easy and there will be no need to recalculate it each time (which is very important).
To do it you can modify your query to update rows instead of selecting it.
I have one table accounts. I have written following query
chk_account= mysql_query("SELECT GROUP_CONCAT( DISTINCT user_id ) AS userlist
FROM `accounts`
");
From this I get users id only. With this query I also want to fetch data price and date with column name price and created but I need only to select with lowest date
I have table structure like this:
id user_id price created
1 31 10 2013-04-09 17:30:15
2 32 20 2013-04-10 20:24:40
3 31 30 2013-04-11 04:44:25
4 33 40 2013-04-12 05:47:18
5 34 50 2013-04-13 19:54:15
6 34 50 2013-04-14 14:27:15
7 35 10 2013-04-15 13:54:45
8 35 60 2013-04-16 12:24:35
9 35 10 2013-04-17 20:34:10
I suspect that you want the earliest date and price for each user. You can do this using group_concat(), using a query such as:
select USER_ID,
substring_index(group_concat(price order by created), ',', 1) as price,
min(created)
from accounts a
group by user_id
I need to find the length of time between 2 dates (datetime types) based on the unique ID. For example, I have the following data:
ID CallID Starttime
1 56 2011-01-04 10:00:00.000
1 67 2011-03-20 12:20:00.000
1 70 2011-04-08 15:00:00.000
2 57 2011-01-14 11:00:00.000
2 62 2011-02-14 11:00:00.000
2 64 2011-02-15 11:00:00.000
2 75 2011-04-14 11:00:00.000
2 78 2011-05-14 11:00:00.000
I need to find the length of time for all the CallIDs for each ID based on the previous call date (starttime). For example, I need the length of time for all the calls for ID 1 (CallID 67 - CallID 56 and CallID 70 - CallID 67, etc.).
I know I need a loop of some kind that would go through the IDs to find the CallIDs for that ID but do I also need a temporary table where I would organize the CallIDs?
Any suggestions would be appreciated. Thank you.
One way.
;WITH T
AS (SELECT ID,
CallID,
Starttime,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Starttime) RN
FROM YourTable)
SELECT t1.ID,
t1.CallID,
t2.CallID,
DATEDIFF(HOUR, t1.Starttime, t2.Starttime)
FROM T t1
INNER JOIN T t2
ON t1.RN = t2.RN - 1
AND t1.ID = t2.ID
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