If I have a table: ID, num,varchar
where ID is an integer, either 1,2 or 3.
then num is a number, counting from 1 to 100.
varchar is just some text.
So in total we have 300 rows, in no particular ordering in this table.
What query can I use to get the rows with ID=2 and num from 16-21 out of this table?
(resulting in 6 rows total)
How about
SELECT * from yourtable where ID = 2 AND num >= 16 AND num <= 21
Or, equivalent using BETWEEN
SELECT * from yourtable where ID = 2 AND num BETWEEN 16 AND 21
Create an index to have faster lookups later (but will slow down your inserts a bit):
CREATE INDEX indexname on yourtable (ID,num);
SELECT * FROM TABLE WHERE ID = 2 AND NUM > 15 AND NUM < 22;
where TABLE is the name of your table. In general, given that you're selecting on columns ID and NUM they should probably be indexed for faster retrieval (ie the database doesn;t have to check every row). Although given your table is small it probably wont make much difference here.
This should do it:
SELECT * FROM table WHERE id = 2 AND num > 15 AND num < 22
Related
Say you have an inventory journal table with these fields:
ID, ProductID, WarehouseID, etc.
ID = PK
ProductID & WarehouseID are both FK and indexed.
Then let's say we populate 5 million rows of data into the table. I ran 2 queries.
The first query used both FKs ProductID and WarehouseID
SELECT inventoryjournals.id,inventoryjournals.ProductID
FROM zenlite.inventoryjournals
where productid = 1 && WarehouseID = 1
limit 30 offset 2500000
This took 5.75s to return the result understandably because it goes through from 1st record to 2.5 mill record. But then I ran another query with arbitrary ID constraint
SELECT inventoryjournals.id,inventoryjournals.ProductID
FROM zenlite.inventoryjournals
where productid = 1 && WarehouseID = 1 && id <10000000
limit 30 offset 2500000
or even this
SELECT inventoryjournals.id,inventoryjournals.ProductID
FROM zenlite.inventoryjournals
where productid = 1 && WarehouseID = 1 && id > 0
limit 30 offset 2500000
This shrank the time down to 1.5 ~ 1.6s?! Does this mean it's always better to add the PK constraints in all read queries? Like id > 0 (always true)
My question is, will doing this pose any risk?
There is no way to make offset 2500000 run fast. It must skip over that many rows (unless it hits the end of the table).
All 3 of your queries could benefit from
INDEX(production, WarehouseID, id)
Large offsets are a poor way to do "pagination". It is better to "remember where you left off". Or are you using the large OFFSET for some other purpose?
My problem is how to loop through a table and extract information from another table.
I have a table - X with 470 records:
A B C
111 12 18
121 21 29
127 37 101
I would like to write the following query:
create or replace view NEW as
For j = 1-3
Select * from Y
where imei = X.A(j) and id > X.B(j) and id < X.C(j)
Apologies, I am a matlab programmer so I have used that syntax above to explain what I want. How can I do this in MySql? I have looked up For Loops but mostly it loops through within the same table. I need to loop through a different table and use those criteria in the where statement of a different table.
To get 3 rows from a table, use LIMIT 3 in a subquery. To get related rows in another table, use JOIN.
CREATE OR REPLACE VIEW new AS
SELECT Y.*
FROM Y
JOIN (SELECT *
FROM X
LIMIT 3) AS X ON Y.ime1 = X.a AND Y.id > X.b AND Y.id < X.c
To make LIMIT 3 produce predictable results, you should have an ORDER BY clause in the subquery. Otherwise, it will select an arbitrary set of 3 rows from X.
For Example our table;
İd--------Price---------Level
1 ------100-300 ------ 1,2
2 ---------200----------1
3 ------100-280--------1,3
We want search a price value is 110. 110 is between 100-300 and 100-280 so id 1 and id 2 must listed. Can we write this query with my-sql?.
Additional , we want search price and level value. Price 110 and level 2 searching. Can we write this query with my-sql?.
Thank You
Remember that database tables should be created with the idea that it will satisfy your query needs. It doesn't make sense to have a table with a price "100-300" which represents a String (or in mysql a VARCHAR) and you want to treat this as a number. So what to do?
1) The first thing i would do is re write my table schema having a minPrice and maxPrice fields, so this way you could have this:
İd----minPrice---maxPrice------level
1 ------100---------300 ------ 1,2
2 ------200---------200----------1
3 ------100---------280--------1,3
2) Then your query would be like:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice
3) In case you also want to look for a level value. you would do:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice AND myLevelValue IN (x.level)
Use two columns for the price ...
ID FROM TO LEVEL
1 100 300 1,2
2 200 200 1
3 100 280 1,3
Then SQL:
SELECT `level` FROM `table`
WHERE X >= `from` AND X <= `to`
When using the <= in a MySql statement, is it possible to make MySql select the maximum value in a table without supplying a value to <= in the sql statement?
Eg:
id
----
1
2
3
4
5
6
Eg:
// start from the last record when no value is supplied
select id from table where id <= * ORDER BY id DESC LIMIT 5
Result
6
5
4
3
2
// start from the 5th record when a value is supplied
select id from table where id <= 5 ORDER BY id DESC LIMIT 5
Result
5
4
3
2
1
My problem is, this statement is in a php function, so I cannot change it dynamically. Is it somehow possible to do what I'm trying, or perhaps another way to get around this?
You can use NULL instead of using *
SET #var_search_value = NULL -- OR 5
SELECT id
FROM table
WHERE id <= #var_search_value OR #var_search_value IS NULL
ORDER BY id DESC LIMIT 5
If you want to get every record which is less than or equal to the maximum value in a particular column, then logically you want to get every record:
select id from table ORDER BY id DESC LIMIT 5
No WHERE clause is required.
I need to get 1-2 rows from query result retrived with SQL select on indexed columns without getting the whole record set.
For example I will retrieve 10 000 records using query
SELECT * FROM table WHERE field 1>1 AND field1 < 10
but I need only 1 random row from this query regarding to highload of my database.
I can use
SELECT * FROM table WHERE field 1>1 AND field1 < 10 LIMIT 100, 1
But I don't know records numebr to use correct offset range
How can I achieve this goal?
You could use ORDER BY RAND()
SELECT * FROM table WHERE field1 > 1 AND field1 < 10 ORDER BY RAND() LIMIT 1
This will return 1 random row with field1 in between 1 and 10
How about restricting the records you select in the first place?
SELECT * FROM table WHERE field1 IN (CONVERT(RAND()*10,SIGNED),CONVERT(RAND()*10,SIGNED)) LIMIT 2