Hello I want to get select query with send row record number
$row = 3;
SELECT FROM clients WHERE ROW()=$row ORDER BY ID DESC
it is possible ? How can i do that ?
If you want the third row, use offset/limit:
select *
from clients
order by id
offset 2
limit 1;
Note that that offset 0 gets the first record, so offset 2 would be the third record.
You need to use LIMIT instead of WHERE
If you want get a row with N position, you can try this:
SELECT * FROM clients LIMIT N-1,1
So if you want get third row you need to use something like this:
SELECT * FROM clients LIMIT 2,1
This is what i would do.. entry_id is unique and 1 = first row, 2 = second row, etc..
entry_id is set as primary index and auto increase..
entry_id | what | ever | records
1 | a | b | c
2 | a | b | c
3 | b | c | a
4 | a | b | c
5 | a | b | c
$row = 3;
Select * From clients Where entry_id = $row
returns third row, 3, b, c, a
Related
I have created an SQL table to save events data into it.
Each event can have multiple occurrences and when I filter them on site - I want to have the first matching occurrence of each event. each occurrence is saved in a different row, which contains a column for the general event_id and specific occ_id to each occurrence.
I need to get from the matching rows - only one row from each event_id, and it needs to be the one with the lowest occ_id value.
i.e.
gen_id | event_id | occ_id | month
------------------------------------
1 | 190 | 1 | 4
2 | 190 | 2 | 4
3 | 190 | 3 | 4
4 | 192 | 1 | 4
5 | 192 | 2 | 4
6 | 192 | 3 | 4
7 | 193 | 1 | 5
8 | 193 | 2 | 5
If I'm looking for events from month = 4, I need to get the events (gen_id): 1,4
and if I'm looking for month = 5 I need to get only event (gen_id): 7
My SQL query right now gets the matching events but with no occ_id filteration:
(it looks something like this right now)
SELECT
event_id,
event_title,
occ_id
FROM
table_name
WHERE month = 4
GROUP BY event_id
ORDER BY
event_id
DESC
I have tried to also use MIN / MAX but I guess it either not the right handler for this case or I'm using it wrong...
You want to filter. One method uses a correlated subquery in the WHERE clause:
select t.*
from table_name t
where t.occ_id = (select min(t2.occ_id)
from table_name t2
where t2.event_id = t.event_id
);
However, the lowest value always seems to be "1", so this might work as well:
select t.*
from table_name t
where t.month = 4 and
t.occ_id = 1;
To add month, you can add it to the outer query:
select t.*
from table_name t
where t.month = 4 and
t.occ_id = (select min(t2.occ_id)
from table_name t2
where t2.event_id = t.event_id and
t2.month = t.month
);
umm,
select t.event_id, t.occ_id, t.month, min(t.gen_id) from (
select event_id,month,min(occ_id) as min_occ_id from t where month=5 group by event_id, month
) t1 join t on t1.min_occ_id = t.occ_id and t1.event_id = t.event_id and t1.month = t.month
group by t.event_id, t.occ_id, t.month;
if columns event_id,occ_id,month make up an UNIQUE KEY, the SQL can be simplified.
select t.event_id, t.occ_id, t.month, t.gen_id from (
select event_id,month,min(occ_id) as min_occ_id from t where month=5 group by event_id, month
) t1 join t on t1.min_occ_id = t.occ_id and t1.event_id = t.event_id and t1.month = t.month
I have a huge number of rows that I'd like to get say, last 5 records inserted in that database from 10 different users. If the same user inserted the last 3 rows into database, we must get one row, skip the others two and move to get a row per user, until it count up to 5.
A database like that:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
1 | 4 | baa
4 | 5 | baa0
5 | 6 | baa1
5 | 7 | baa2
6 | 8 | baa3
7 | 9 | baa4
Should return:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
4 | 5 | baa0
5 | 6 | baa1
The current filter was done by PHP, like this:
$used = array();
while ($data = mysql_fetch_array($query)) {
$uid = $data['user_id'];
if(in_array($uid, $used))
continue;
array_push($used, $uid);
// do something with data
}
But I want to refactor it, and do the filter purely by mysql, if possible. I don't know much MySql and that's why I'm having problem to archive this...
Here's what I've tried
select DISTINCT(user_id), news_id, title from XXX
WHERE GROUP BY (news_id) DESC
LIMIT 0,5
How can I do that?
1 way you can do it is to generate a partitioned row number per user and then select 5 records where RowNumber = 1.
SELECT *
FROM
(
SELECT
d.user_id
,d.news_id
,d.title
,(#rn:= if(#uid = user_id, #rn + 1,
if(#uid:=user_id,1,1)
)
) as RowNumber
FROM
Data d
CROSS JOIN (SELECT #uid:=-1, #rn:=0) vars
ORDER BY
user_id
,news_id
) t
WHERE
t.RowNumber = 1
ORDER BY news_id
LIMIT 5;
http://rextester.com/JRIZI7402 - example to show it working
Note you can change the row order by simply changing the ORDER BY statement of the derived table so if you have a column that will signify the latest record e.g. an identity column or a datetime column you can use that, but user_id must be the first criteria to be partitioned correctly.
Do it from your query.
"SELECT * FROM table GROUP BY user_id ORDER BY news_id DESC LIMIT 5"
well, i think this will achieve what you are after.
select user_id, news_id, title from tableName
GROUP BY user_id
ORDER BY news_id DESC
LIMIT 0,5
Hope this helps!
I found another thread with this similar question but the query wasn't working for me for some reason or another.
I have a table like so:
id | 1 1 5 3 5
I need to use just SQL to echo out the most duplicated number
For instance, that would output:
id | 1 5 3 5
If that makes sense.
How would I achieve this?
Thank you so much
RETURNING JUST UNIQUE ID's
SELECT DISTINCT id FROM myTable
ID |
---------------
1 |
5 |
3 |
See DEMO
RETURNING JUST THE MOST DUPLICATED ID WITH COUNT
SELECT id, COUNT(id) AS Duplicates FROM test
GROUP BY id
ORDER BY Duplicates DESC
LIMIT 1;
ID | Duplicates
---------------
1 | 2
// without the LIMIT clause
ID | Duplicates
---------------
1 | 2
5 | 2
3 | 1
See DEMO
Or, as you see above, there may be TWO ID's that have been duplicated the same amount of times. You could do this, which would return both the highest duplicated ID's, if they're equal:
SELECT id, COUNT(id) AS Duplicates
FROM test
GROUP BY id
HAVING COUNT(id) = (
SELECT COUNT(id) AS great
FROM test
GROUP BY id
ORDER BY great DESC
LIMIT 1
)
ID | Duplicates
---------------
1 | 2
5 | 2
See DEMO
RETURNING JUST UNIQUE ID's IN PURE PHP
$results = // query
$results = array_unique($results);
Use a group by together with a count, like this:
select * from t group by id order by count(id) desc
And add a limit clause in order to get the single most duplicated value:
select * from t group by id order by count(id) desc limit 1
Suppose I have the next table structure, table A:
| id |
|----|
| 3 |
| 4 |
| 7 |
Table B:
| id | title | last_id |
|----|-------|---------|
| 1 | a | 1 |
| 2 | b | 2 |
| 3 | c | 3 |
I want to change Table's B last_id to the next matching id from Table A.
Example: (Referring to Table's B first row)
Current last_id is 1, So I need to forward it's value to the next, bigger id from Table A.
So it will go:
1 -> 3 -> 4 -> 7
It can be done also with PHP, but maybe there is a way to keep it in the SQL Server.
Thanks in advance.
NOTE: The server runs MySQL.
Previous answer is correct, but the +1 I believe is no needed as you just need value 7
UPDATE table_b
SET last_id = (SELECT MAX(id) FROM table_a)
Edited:
Now it picks the minimum value from table_a bigger that the last_id on table_b
UPDATE table_b
SET last_id = (SELECT MIN(id) FROM table_a WHERE id>last_id)
The following snippets are pseudo code.
Grab the next table_a id:
SELECT id FROM table_a WHERE id > $currentLastId ORDER BY id ASC LIMIT 1
If not null, update your table_b last_id column.
Example: (Referring to table_b first row)
The SQL query would be:
SELECT id FROM table_a WHERE id > 1 ORDER BY id ASC LIMIT 1
It would return 3.
I have this table: I want to search by UID
ID | VID | UID
1 | 1 | 5
1 | 1 | 6
1 | 2 | 6
2 | 3 | 5
2 | 3 | 6
2 | 4 | 6
I want to end up with this result:
ID | VID | UID
1 | 2 | 6
2 | 4 | 6
In other words, only select the entries where the VID is MAX of the UID but keeping in min NID could differ. Something like this I suppose:
select * from TABLE where uid = 6 and max(vid)
???
But this doesn't work?
One way is to order by the value in descending order (so the max is at the top), then just select the first result.
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
ORDER BY t.VID DESC
LIMIT 1
Or do you mean you want all rows where t.VID is the highest value? In which case you could do something like this,
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
AND t.VID = (SELECT MAX(VID) FROM table);
EDIT: Based on the edit to your question, it looks like you just want the max VID value for each ID? If I'm understanding you correctly, then this should give you what you need.
SELECT t.ID,
max(t.VID) as VID,
t.UID
FROM table t
WHERE t.UID = 6
GROUP BY t.ID
You need to have a subquery. This should work:
select * from TABLE where ID='1' AND VID=(select max(VID) from TABLE)
I expect your real-life example is more complicated (at least has more data).
This query will give you the row you want.
SELECT id,vid, uid
FROM TABLE
where id = 1
and vid in (select max(vid) from TABLE where id = 1)