I am trying to select chunks of 5 rows every time I make a database call.
In my DB, some columns might have been deleted and others not, so the ID column can have "gaps", like missing numbers, something like this:
ID name category ....
33 xxxx xxxxxxx
34 xxxx xxxxxxx
38 xxxx xxxxxxx
40 xxxx xxxxxxx
41 xxxx xxxxxxx
45 xxxx xxxxxxx
49 xxxx xxxxxxx
...
I have ben trying with something like this:
$query = "SELECT * FROM Products LIMIT 33, 5";
I understood that adding LIMITto the query it will take the next following 5 rows starting by 33, however it does not seem to be working.
Any better suggestion to get 5 rows starting by a specific ID each time??
If you read manual for limit clause, you will see definintion:
LIMIT {[offset,] row_count ...
So in your case LIMIT 33, 5 means SKIP 33 records and TAKE 5 following records, and not FIND RECORD WITH ID = 33 and TAKE 5 following records.
So, your option is to use a where-clause:
SELECT * FROM Products WHERE ID > 33 ORDER BY ID ASC LIMIT 5
The values that follow the LIMIT keyword are the offset and the number of rows from the complete recordset to return. They are not IDs or other values returned by the query.
Read about MySQL SELECT statement.
If you want to get 5 rows starting with the one having ID 33 then your query is:
SELECT *
FROM Products
WHERE ID >= 33
ORDER BY ID
LIMIT 5
Without LIMIT 5, the query above returns all the rows having ID greater than or equal with 33, sorted in ascending order by their ID. The LIMIT 5 clause (which is a short for LIMIT 0, 5) tells MySQL to return only the first 5 rows from the list.
Related
I have a table "student_points".
id user_id points subject_id
1 10 45 22
2 11 75 23
3 12 78 24
4 10 13 23
5 12 65 23
and so on ...
This table contain about 1000 records of users and points.
I want 10 records based on points (Max points first)
So I can use mysql query as
Select * from student_points order by points limit 0,10
Now the requirement is that we need to group these 10 records based on user_id
For example in first 10 records three are 3 students records so they should display in group.
End result should be like
id user_id points subject_id
3 12 78 24
5 12 65 23
1 10 45 22
4 10 13 23
2 11 75 23
You can see that first record is based on most point and it student id is 12, now they are group according to user_id.
I tried two order by .
I also tried to array_multisort after getting result but both are not working properly.
Please suggest any way, Either mysql query or group after getting result.
Thanks
To get your required result I have written the query :
SELECT * FROM (
Select * from student_points order by points limit 0,10
) As st
GROUP BY user_id,subject_id
ORDER BY points DESC
Please try this. Let me know if this is not work for you.
This should work just add a limit to whatever number you want to limit by
select sp.id, sp.user_id, sp.points
from student_points sp
join (select user_id, max(points) as sort_by from student_points group by user_id) sort_table on sp.user_id = sort_table.user_id
order by sort_table.sort_by desc, sp.user_id, sp.points desc;
I guess you need to wrap up your query in a subquery and later sort the results based on user_id and points.
SELECT * FROM
(
Select * from student_points order by points limit 0,10
) AS t
ORDER BY
t.user_id DESC,
t.points DESC
I have a table that is something like the one below.
id | episode_number
1 55
2 56
3 57-58
4 59
5 60
6 61-62
7 63
8 64
9 65-66
10 67-68
How would I get the next 5 episodes after 57-58 without specificity the id, just the episode_number such as episode_number > 57-58 LIMIT 5.
But obviously episode_number > 57-58 would not work since it contains string in it. The database will just consider it as 5 and return the next 5 results after the episode_number 5.
Use CAST function to convert string to number, then your query:
SELECT episode_number
FROM table_name
WHERE CAST(episode_number AS UNSIGNED) > CAST('57-58' AS UNSIGNED)
ORDER BY CAST(episode_number AS UNSIGNED)
LIMIT 5
If there are two numbers, for example: '57-58', then CAST('57-58' AS UNSIGNED) converts and returns first one: 57
Try
SELECT TOP 5 * FROM tablename WHERE id >= (SELECT id FROM tablename WHERE episode_number = "57-58")
or if it's MySQL
SELECT * FROM tablename WHERE id >= (SELECT id FROM tablename WHERE episode_number = "57-58") LIMIT 5
You can select like this:
select id , case when LOCATE('-' , episode_number) = 0 then episode_number
else LEFT(episode_number , LOCATE('-' , episode_number)) end
as start_episode ,
case when LOCATE('-' , episode_number) = 0 then episode_number
else RIGHT(episode_number , LEN(episode_number) -LOCATE('-' , episode_number)) end
as end_episode
this should give you the base structure for such queries, you should consider converting the values into numbers to be able to easily compare them.
If possible restructure the table to something that has the start episode and end episode in separated fields
Hi Im trying to get certain amount of rows after the first 3 rows like for example:
this is the rows in the database
-id
20
19
18
17
16
15
When i select * from #__table where published=1 order by id desc limit 0,3
i get
20
19
18
as result.
Now what i want to do is create a query where i can get the next ones after the first 3 rows
17
16
15
Try to increase your limit with 3
SELECT *
FROM #__TABLE
WHERE published=1
ORDER BY id DESC LIMIT 0,3
Then LIMIT 3,3
Next LIMIT 6,3
What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid
I am rather new to MySQL, I have this problem, which I have searched for help with, but could not find the answer (possibly because I am not sure what to search for). I am sure there is a simple solution.
I have a simple MySQL table like this:
id | referenceid | status
40 104702 4
39 104720 2
38 104720 0
37 104719 2
36 104719 0
35 104702 2
34 104702 0
41 104719 5
I want to fetch an array that will produce the following output:
For each unique "referenceid" field, I want to know the highest "ID" (ie most recent) and its "Status".
So I have tried this query:
$result = mysql_query(
"SELECT status,id,referenceid
FROM image_status_tracking
GROUP BY referenceid",$db);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$status = $row['status'];
$referenceid = $row['referenceid'];
echo "ID $id, REFID $referenceid, Status $status <br />";
}
which gives this output:
ID 40, REFID 104702, Status 4
ID 37, REFID 104719, Status 2
ID 39, REFID 104720, Status 2
But this is not the result I was hoping for. The ID and Status for referenceid 104719 should be 41 and 5, not 37 and 2.
I cannot work out how to get the most recent ID line output for each referenceid.
Something I noticed, in PHPMYADMIN, (and you can see in the table above), the IDs are listed in reverse order, with the latest ID at the bottom. I am not sure if that is relevant to this problem.
I hope this is clear, I am sure it is a simple matter.
Thank you for reading.
James
Typing from memory so you might have to correct the syntax a little bit but the concept, i think, should work.
select *
from image_status_tracking
where id in (
select max(id) from image_status_tracking
GROUP BY referenceid
)
Your query does not do what you want it to do. For each reference id, it is selecting a random id and status. This is a (mis)Feature of MySQL called Hidden Columns.
What you want is more like:
select ist.*
from image_status_tracking ist join
(SELECT referenceid, max(id) as maxid
FROM image_status_tracking
GROUP BY referenceid
) r
on ist.referenceid = r.referenceid
Try this...
SELECT MAX(ID), Status From image_status_tracking
GROUP BY referenceid