I'm using this query to select a set of records from a MySQL database:
SELECT *
FROM table
WHERE id IN(10,14,12,11,8,7,4)
AND actief='on'
ORDER BY FIELD(id,10,14,12,11,8,7,4)
This will give me the given ID's. In this case I will get a maximum of 7 records. But it can be less if for example ID '14' has active='off'.
But what I need is a set of 20 records where het list IN(10,14,12,11,8,7,4) must be in the result if they also meet the condition active='on'. If this returns 6 records, then I want the query to select another 14 records. Selecting highest ID first, must meet active='on' and may not already be in the result.
Can this be achieved by one SQL statement. Or should I first put the result of the mentionend query in an array and in a second array select the remaining records. And finaly put those also in the array?
You want to sort rather than filter the results. I think this is the query you want:
SELECT *
FROM table
ORDER BY (id IN(10,14,12,11,8,7,4) AND actief = 'on') desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
EDIT:
The final solution only wanted actief = 'on', so:
SELECT *
FROM table
WHERE actief = 'on'
ORDER BY (id IN (10,14,12,11,8,7,4)) desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
Related
I have a table that has multiple categories (c) & I'd like to select 3 random rows for each category each time I run this query. I got it to select 3 just fine but noticed it wasn't selecting random rows, but instead the same rows each time. So I'm back to square one with my query.
select PLAYERID,
NAME,
RACEID,
VALUE,
MA,
ST,
AG,
LEVEL,
SKILLS,
XP,
TYPE
FROM FAPLAYER
GROUP BY TYPE
ORDER BY RAND()
I'm trying to get 3 of each TYPE from the FAPLAYER table where I have probably around 50 different TYPE's.
Here's my query output for a basic query with RAND
Solution 1: Looks like "GROUP BY TYPE" is giving you trouble. Try this:
SELECT * FROM FAPLAYER
ORDER BY RAND()
LIMIT 3;
Solution 2:
Do you have a column with ID which auto increment? If so then you can do the following max=50, min=1:
SET #rand_id = (SELECT FLOOR((RAND() * (max-min+1))+min));
SELECT *
FROM FAPLAYER
WHERE RACEID = #rand_id
I would like to select two specific values, the first value is the last inserted row where the ID_SENSOR is 1, and the second value is the last inserted row where the ID_SENSOR is 2.
My Database table:
My Query:
SELECT DATA FROM (SELECT * FROM registovalores WHERE ID_SENSOR = '1' OR ID_SENSOR = '2' ORDER BY ID_SENSOR DESC LIMIT 2) as r ORDER BY TIMESTAMP
My Query is printing the last value just from the ID_SENSOR 1, which it means that I'm only getting the last inserted values, and not the last inserted value from both IDS.
I would like to print my values like this:
ID_SENSOR 1 = 90
ID SENSOR 2 = 800
What do I need to change on my Query?
Thank you.
One method uses a correlated subquery:
SELECT rv.*
FROM registovalores rv
WHERE rv.ID_SENSOR IN (1, 2) AND
rv.TIMESTAMP = (SELECT MAX(rv2.TIMESTAMP)
FROM registovalores rv2
WHERE rv.ID_SENSOR = rv2.ID_SENSOR
);
You have to have two separate queries, one per sensor.
select id_sensor, data
from the_table
where id_sensor = 'sensor_1'
order by timestamp desc -- the latest value is the first to come
limit 1; -- only pick the top (latest) row.
If you want to query for more than one value in a single database roundtrip, consider using union all between several such queries.
Please note that such a query may return one row or zero rows, since data for a particular sensor may not be available yet.
I have a "reply" table with structure.
replyno topicno replydesc replyrank
Now i need to retrieve top 2 records ordered by replyrank in descending order (means first 2 high ranked records) for each topicno (which is a foreign key).
I need a query in mysql that can extract result set like this for all topic numbers.
please give me optimized query that can execute faster
Try this:
SELECT replyno, topicno, replydesc, replyrank
FROM (SELECT replyno, topicno, replydesc, replyrank,
IF(#topicno = #topicno:=topicno, #id:=#id+1, #id:=1) AS id
FROM reply, (SELECT #id:=1, #topicno:=0) A
ORDER BY topicno, replyrank DESC
) AS A
WHERE id <= 2;
Try this Query i think it gonna work for you
SELECT replyno, topicno FROM reply ORDER BY replyrank DESC LIMIT 2
Here is my DB data's sample:
both two columns are int
tuanId ,tuanSort
'375579', '55'
'370576', '54'
'366222', '54'
...
'346268', '52'
'369608', '52'
'370587', '52'
'370775', '52'
...
'370225', '52'
'370588', '52'
'360758', '52'
'366390', '51'
and I try these sqls bellow:
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 140,20;
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 160,20;
and I get these wrong data, I want to make a pagination, but the second page has some same data in the first page:
For Example, the first pic's 17th row has shown twice in the 2 pics
So, is the sort value the same can cause such a problem? Or MySQL has problem with such a select?
Given that tuanSort is not unique, the behavior is within the specification.
You are observing that one query returns a particular row as 157th row. In another query execution, it's returned as the 161st row.
To get more deterministic sequence, specify additional columns in the ORDER BY clause, e.g.
ORDER BY tuanSort DESC, tuanId DESC
If the intent behind this sequence of statements is "paging", there are more efficient approaches, such as saving a unique, sequenced identifier from the "last" row of the previous page.
If tuanSort,tuanId tuple is unique...
WHERE tuanSort <= :last_tuanSort
AND ( tuanSort < :last_tuanSort OR tuanId < :last_tuanId )
AND ...
ORDER BY tuanSort DESC, tuanId DESC
LIMIT 20
If you fetch all twenty rows, save tuanSort and tuanId from that last row. On the next "page", supply those saved values in the query predicates.
But that's an answer to a question you didn't ask.
I think you need to refresh your database table.
Try this : Go ->TuanItem->Click Operations->Alter table order by-> select your tuanid and set as an ascending order.
I think your problem is, your getting same data in 2 query.
then change the limit 160,20 to
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 140,0;
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 160,141;
the 0 is the offset which means the data start at 0 and the second query start at the 141 so that you cannot get same value twice
I got two MySQL working fine and i'm trying to find a way to combine them into one single query.
First, it selects ID of an employee.
SELECT 'ID' FROM `employee` ORDER BY ID DESC LIMIT 1;
Let's say it returns ID 100;
Then update data of employees whose ID is 100
UPDATE 'LOG' SET `TIME_EXIT`='2013/02/22' WHERE `ID`='100';
Can i do it all in a single query?
Just add them together:
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
ORDER BY ID DESC
LIMIT
);
But based on that code currently it'll only ever update the last employee, you will need to select the correct employee by using some other identifier to ensure you have the correct one.
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
WHERE NAME = 'JOHN SMITH'
ORDER BY ID DESC
LIMIT 1
);
It's now a few months old, but maybe helps you or others finding this via google…
If you want to UPDATE a field in the same selected table use this:
UPDATE LOG SET
TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM (
SELECT ID
FROM LOG
WHERE whatEverYouWantToCheck = whateverYouNeed
) AS innerResult
)
So, you SELECT id from a subselect. If you try to subselect it directly, mySQL quites with your error message You can't specify target table 'log' for update in FROM clause, but this way you hide your subsubquery in a subquery and that seems to be fine. Don't forget the AS innerResult to avoid getting the error message #1248 - Every derived table must have its own alias. Also match the subsubquery field name to the subquery field name in case you do something like SELECT COUNT(*) or SELECT CONCAT('#', ID)