I have this query:
select distinct Id,srcId
from schedule_mid as m
where Id > 100 and
not EXISTS ( select 1 from schedule_detail where id = m.srcId )
order by srcId
limit 500
In my opinion: first select one columns's Id and srcId
then look whether Id > 100 and not,,,,,,,,
then look whether have 500 numbers
if( >=500 ) then order by srcId and break;
revert first
I just guess the result but i don't know whether right or wrong;
The answer is: it depends.
Read this for more information:
click!
Related
What I'm trying to do is get the Next and Previous values in SQL.
In this example I'd always know the current value.
This is how my table is laid out.
id
parentID
appID
Name
19410
18371
2da4
name600
19410
18371
4ac0
name24
19410
18371
348e
name441
So my goal is for example get the next/previous value from the current.
So if the current is test2, I'd get test3 back.
I have looked into using offset + limit but I don't think that allows you to select a current starting point.
I cannot store an array as I don't want it to be slow either.
This differs from other questions as I do not have a iterable value as for example name won't always be test1, test2, test3.
My version is 8.0.19 - MySQL Community Server - GPL
For efficiency, you will need INDEX(name) (or some index starting with name)
To get the next row based on "name":
SELECT ...
FROM ...
WHERE name > 'test2'
ORDER BY name ASC
LIMIT 1
For Previous, change 2 things:
SELECT ...
FROM ...
WHERE name < 'test2'
ORDER BY name DESC
LIMIT 1
To get both at the same time:
( SELECT ... FROM ... WHERE name > 'test2' ORDER BY name ASC LIMIT 1 )
UNION ALL
( SELECT ... FROM ... WHERE name < 'test2' ORDER BY name DESC LIMIT 1 )
This will be a lot faster than LEAD and LAG or Cursors.
I running into some time issues using my simple select:
SELECT *
FROM ltowert
WHERE bat = 3
AND id >= (SELECT id
FROM ltowert
WHERE bat = 3 AND ident = 'v0'
ORDER BY id DESC
LIMIT 1)
ORDER BY ident;
It takes nearly 12 seconds (depending on the index, etc..)
If I run the subquery (0.00075 sec) and put the result in the statement:
SELECT *
FROM ltowert
WHERE bat = 3 AND id >= 20979399
ORDER BY ident;
it runs in just 0.00095 sec, in addition: 0.0017 sec
So it seems, using the subquery avoid the Optimizer to use the index ?
How can I fix it and get quicker results ?
Thanks for any answers.
JR
I test your proposal, but I run into a problem of understanding. Maybe, it's my issue:
Your idea works, but the result is wrong: Result is only 1 row, but I expect 11 rows. I expect all row from l1, where id >= l2.id.
So I changed to:
SELECT *
FROM ltowert l1
INNER JOIN (
SELECT id
FROM ltowert
WHERE bat = 3 AND ident = 'v0'
order by id desc limit 1
) as l2 on l1.id>=l2.id
order by l1.ident;
This return much rows. l1.bat = 0 which is not the request.
Change to:
A. ") as l2 on l1.id>=l2.id AND l1.bat = 3"
or
B. WHERE l1.bat = 3
runs into a timeout Error 2013.
What is my mistake ?
Additional, due to my 2 step tests, I do a test using a variable:
SELECT #ref_id:=id FROM ltowert WHERE bat = 3 AND ident = 'v0' order by id desc limit 1;
SELECT * FROM ltowert l1 where bat = 3 AND l1.id >= #ref_id order by l1.ident;
It is fast, and give the right results.
Is there a disadvantage to this usage? (I know: 2 separate statements were not maintenance friendly)
You need both of these:
INDEX(bat, ident, id)
INDEX(bat, id)
If they don't suffice, please provide
SHOW CREATE TABLE ltowert;
EXPLAIN SELECT ...;
I know that I'm using wrong title because I can't think of a proper term for my problem. I am doing a document tracking system.
Here is the problem: the document need to be signed by the lower personnel first before the next personnel who is in a higher position.
In order to do that, I came up with the idea of using level on my table.
But I lack MySQL knowledge. This is my MySQL table:
This is my MySQL query. The problem is, I don't want to show to
LHPL003 the document yet because the other signatorylevel lower than his has not yet signed the document (colum status will become 0 if the signatory signed the document).
How can I do that?
SELECT `tracknum`, `signatoryid`, `signatorylevel`, `status`
from tble_transaction
where signatoryid = "LHPL003" and signed = 'Released' and signatorylevel <= "3";
question number 2:
i have problem with my first question
now i have this table
how can i do this. example the user with the level 1 already signed the document so it become
LHPL005 status = 0
what i want is a query that only LHPL004 can see the document
LHPL003 and LHPL002 cant see the document first because those who have lower lever than theirs didn't signed yet the document
SELECT `tracknum`, `signatoryid`, `signatorylevel`, `status`
from tble_transaction
where signatoryid != "LHPL003" and signed = 'Released' and status='0' and signatorylevel <= "3";
To display the rows having status as 0 and having minimum signatory level (which is input from the user)
SELECT tracknum, signatoryid,signatorylevel, status from track where status=0 and signatorylevel < 4 order by signatorylevel limit 1;
EDIT:
The above query will result only 1 record if you want multiple records use the following query
select * from track where signatorylevel = (select min(signatorylevel) from track where status = 0 and signatorylevel < 4 );
I do not understand exactly what you want, But I think one of those 2 queries has the solution for you:
SELECT * FROM `tble_transaction` as t1
WHERE signatoryid = 'LHPL003' and signed = 'Released'
AND EXISTS (select 1 from tble_transaction where signatorylevel < t1.signatorylevel and status = 0)
Second one:
Select * from tble_transaction where
signatorylevel < (SELECT min(signatory level) FROM `tble_transaction` as t1 WHERE signatoryid = 'LHPL003' and signed = 'Released' and status = 1)
ORDER BY signatory level desc
Limit 1
Second question:
SELECT * FROM `tble_transaction` as t1
WHERE signatoryid = 'LHPL005' and signed = 'Released'
AND NOT EXISTS (select 1 from tble_transaction where signatorylevel < t1.signatorylevel and status = 1)
-- ORDER BY signatorylevel LIMIT 1 -- you can ignore this if signatoryid is UNIQUE
I have a MySQL table. the column are ID, ChId, TotalView...
Suppose I want to get all the rows which have Totalview -5 then me to all +5 then me. I want to search in table for people who get similar views.
How I can write a query to get the all rows.
select * from test where test.chid = 1 and totalview are (-5 then current , +5 then current)
You may try this query
SELECT * FROM test WHERE totalview> (current-5) AND totalview< (current+5);
select * from test where test.chid = 1 and totalview>=(current-5) AND totalview<=(current+5)
use your current totalview in current
select * from test
where test.chid = 1
ORDER BY totalview ASC
LIMIT current-5, current+5
You should calculate "current-5" and "current+5"
Use the query:
SELECT totalview as current_view FROM test WHERE chid = 1
Then substitute current_view from above query:
SELECT * FROM test WHERE totalview BETWEEN current_view - 5 AND current_view + 5
Assume this query chid=1 returns 1 record.
If we're doing this all inside of MySQL, we can create a temporary variable called #current, and then query on the result.
SELECT #current := totalview FROM test WHERE chid = 1;
SELECT id, chid, totalview FROM test WHERE totalview BETWEEN #current - 5 AND #current + 5;
get your currentview in a subquery and use it in the search criteria
SELECT * FROM test, (SELECT totalview AS currentview FROM test WHERE child =1) t1
WHERE (totalview <= ( currentview +5 )
I just want to know how to call after 10 rows in flash column.
I want to call all category = today. Is it possible?
For Example
SELECT * FROM news WHERE category='today' AND flash='true' limit 60
Is this what you're looking for (it's an official solution shown in the MySQL SELECT Syntax)?
SELECT * FROM news
WHERE flash='true'
LIMIT 10, 18446744073709551615;
Update:
After reading your comments, maybe this is what you're looking for:
SELECT *
FROM (SELECT *
FROM `news`
WHERE (`flash` = 'true')
LIMIT 10, 18446744073709551615) `after_flash`
WHERE `after_flash`.`category` = 'today';
Do you mean fetch 60 records from positions 11-70?
SELECT * FROM news WHERE category='today' AND flash='true' limit 10,60
You may need to do an ORDER BY clause as well to sort the data.