Get certain amount of rows after the first 3 php mysql - php

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

Related

PHP sql: select a chunks of rows

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.

How to fetch two tables and sort the data by date?

I need a query that fetches data fron two tables and sort them by date.
Table 1: Invoice
<?php
$query = mysql_query("select * from invoice where customer = 95");
?>
ID Customer Amount Date
1 95 1500 01-Apr-2017
2 95 5500 09-Apr-2017
3 95 22000 10-Apr-2017
4 95 35000 11-Apr-2017
Table 2: Payments
<?php
$query = mysql_query("select * from Payments where customer = 95");
?>
ID Customer Amount Date
1 95 10000 02-Apr-2017
2 95 11000 09-Apr-2017
3 95 22000 11-Apr-2017
4 95 1200 15-Apr-2017
I need output as below:
ID Date InvoiceDR InvoiceCR
1 01-Apr-2017 1500 -
2 02-Apr-2017 - 10000
3 09-Apr-2017 5500 -
4 09-Apr-2017 - 11000
$query = mysql_query("SELECT * FROM (
(SELECT invoice.id, NULL AS invoiceDR, invoice.InvoiceCR, invoice.Date FROM invoice)
UNION ALL
(SELECT NULL AS id, paymentd.InvoiceCR, NULL AS InvoiceDR, payments.Date FROM Payments)
) results ORDER BY Date ASC");
put order by date in end of the query
and change order of date as per your need
You can use "order by" for this purpose. You can set order by ascending and descending (ASC or DESC). Use this query.
select * from Invoice,Payments where '{condition}' order by Invoice.Date ASC , Payemnts.Date ASC
This query will firstly sort the data according to first table(Invoice) and then sorted data will sorted with second table(Payemnts)

Order and sort result mysql or using php array funciton

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

Getting the sum of the column in mysql

I am using user Keevas' example, but asking a different question....
select Master_Code, SUM(Jan), SUM(Feb), SUM(Mar)
from dbo.foobar
WHERE Participating_City = 'foofoo'
GROUP BY Master_Code ORDER BY Master_Code ASC
something like this:
Master_Code sum(Jan) Sum(Feb) sum(Mar) Total
1 4 5 4 13
2 5 5 5 15
How do I get Total value column?
SELECT username,SUM(value) AS SumValue FROM
table GROUP BY username ORDER BY SumValue DESC

How can I select all the values of a database and choose one of them by a percent

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

Categories