LIMIT rows from the latest - JOINED tables - php

I got two tables, customer_ledger and users.
I'm using PHP for my simple log-in program wherein the users input their username and password. Once the account is valid, it will show them their billing history. So I joined the two tables to link the username with the contract number in the other table.
My problem now is, how to display their (5) latest billings based on the field (LDGR_PER_COV_TO)? I created a MySQL query but it gave me a different result.
This is my query:
SELECT
customer_ledger.LDGR_YEAR,
customer_ledger.LDGR_MONTH,
customer_ledger.LDGR_PER_COV_FROM,
customer_ledger.LDGR_PREV_RDNG,
customer_ledger.LDGR_PER_COV_TO,
customer_ledger.LDGR_PRES_RDNG,
customer_ledger.LDGR_KWH_USED,
customer_ledger.LDGR_BILL_AMOUNT
FROM
customer_ledger
INNER JOIN users
ON customer_ledger.LDGR_CONTRACT_NO=users.LDGR_CONTRACT_NO
WHERE users.Username = 'kim'
ORDER BY customer_ledger.LDGR_PER_COV_TO ASC
LIMIT 5
this result will give me rows starting from the top most record. I would like it to display from the bottom-going up (latest record - top record).
Can anyone help?

You should use alises for better readibility,
Then use DESC in place of ASC
SELECT cl.LDGR_YEAR, cl.LDGR_MONTH,
cl.LDGR_PER_COV_FROM, cl.LDGR_PREV_RDNG,
cl.LDGR_PER_COV_TO, cl.LDGR_PRES_RDNG,
cl.LDGR_KWH_USED, cl.LDGR_BILL_AMOUNT
FROM customer_ledger AS cl INNER JOIN users AS u
ON cl.LDGR_CONTRACT_NO=u.LDGR_CONTRACT_NO
WHERE u.Username = 'kim'
ORDER BY cl.LDGR_PER_COV_TO DESC LIMIT 5
New Query ::
SELECT * FROM
(
SELECT cl.LDGR_YEAR, cl.LDGR_MONTH,
cl.LDGR_PER_COV_FROM, cl.LDGR_PREV_RDNG,
cl.LDGR_PER_COV_TO, cl.LDGR_PRES_RDNG,
cl.LDGR_KWH_USED, cl.LDGR_BILL_AMOUNT
FROM customer_ledger AS cl INNER JOIN users AS u
ON cl.LDGR_CONTRACT_NO=u.LDGR_CONTRACT_NO
WHERE u.Username = 'kim'
ORDER BY cl.LDGR_PER_COV_TO DESC LIMIT 5
) AS a
ORDER BY a.LDGR_PER_COV_TO

Related

using group by and order by in mysql query correctly

I'm trying to get the latest certificate a user has from the database. I only want to see the latest one and not all the others so I'm using group by and then ordering by the unique id from the main table.
Without group by this works perfectly. I see the last certificate uploaded and then all the others below.
As soon as I add the group by I see the first certificate ever uploaded which is pointless as it could be from years ago.
My query is quite large as I'm drawing in a lot of other information from other tables.
Here is my query.
SELECT
usercert.*,
cert.*,
certCat.certCatName,
certTask.certTaskName ,
certStatus.certStatusName
FROM
`usercert`
INNER JOIN
cert
ON
cert.idcert = usercert.idcert
INNER JOIN
certCat
ON
certCat.idcertCat = cert.idcertCat
INNER JOIN
certTask
ON
certTask.idcertTask = usercert.idcertTask
INNER JOIN
certStatus
ON
certStatus.idcertStatus = usercert.idcertStatus
WHERE
usercert.iduser=%s
GROUP BY
usercert.idcert
ORDER BY
usercert.usercertEnd DESC
SELECT
usercert.*,
cert.*,
certCat.certCatName,
certTask.certTaskName ,
certStatus.certStatusName
FROM
`usercert`
INNER JOIN
cert
ON
cert.idcert = usercert.idcert
INNER JOIN
certCat
ON
certCat.idcertCat = cert.idcertCat
INNER JOIN
certTask
ON
certTask.idcertTask = usercert.idcertTask
INNER JOIN
certStatus
ON
certStatus.idcertStatus = usercert.idcertSttus
WHERE
usercert.iduser=%s
ORDER BY
usercert.usercertEnd
DESC limit 0,1
in this query it will take all the record in descending order it means the last inserted row will come first and the limit 0,1 means it will start from 0 and fetch 1 record that's it ...
You can either use MAX():
SELECT ... FROM ... WHERE ... ORDER BY MAX(usercert.usercertEnd)
Or LIMIT:
SELECT ... FROM ... WHERE ... ORDER BY usercert.usercertEnd DESC LIMIT 1

sorting mysql table by data within a different table

I have to mysql tables and trying to display results of table1 but sorting by table2. For table2 I'm counting all the occurrences of a duplicate id and then display that result descending. Below is as far as I could get and wondering if this can even be done is a single query.
$query = "
SELECT DISTINCT registration.*
FROM registration
INNER JOIN downloads
ON registration.id = downloads.id
GROUP BY downloads.COUNT(id)
ORDER BY downloads.COUNT(id) DESC,
downloads.COUNT(id) DESC
";
I think you want something along these lines:
SELECT Registration.id, Registration.name, Registration.email,
Downloads.document
FROM Registration
JOIN Downloads
ON Downloads.id = Registration.id
JOIN (SELECT id, COUNT(*) as count
FROM Downloads
GROUP BY id) Download_Count
ON Download_Count.id = Registration.id
ORDER BY Download_Count.count DESC
(untested, as it would be nice for the OP to supply sample data and table layouts in the question)

SQL with multiple joins with same tables

I'm building a forum, and I have a problem with a SQL select with many joins. I want to show two images of different users in the same row.
The first image of the user is who wrote the topic, and the second image is of the user who last replied.
The query I build:
SELECT
posts.*, users.photo, users.displayname FROM posts
JOIN users ON(posts.useraid = users.id)
JOIN users ON(posts.lastreply = user.id)
WHERE forumid='$forumid' and type='post' ORDER BY `timee` DESC
posts.lastreply = the ID of the last reply user.
You have to specify an alias for each table using the AS keyword:
SELECT posts.*,
u1.photo AS creatorPhoto, u1.displayname AS creatorName,
u2.photo AS replierPhoto, u2.displayname AS replierName
FROM posts
JOIN users AS u1 ON(posts.useraid = u1.id)
JOIN users AS u2 ON(posts.lastreply = u2.id)
WHERE forumid= #forumid and type='post'
ORDER BY `timee` DESC
Notice how I call each instance of the users table by a different name - u1 and u2. Also notice how I have specified a column alias to distinguish between the two columns of the same name (e.g. creatorPhoto and replierPhoto). This way you can use the name as an index into a PHP associative array a la $post['creatorPhoto'].
Yes, I've silently changed your inline variable to a parameter. Take it as a hint. :-D
In addition to the lack of aliases in the from clause you may also have a problem with the where and order by clause. You need to use aliases for the columns there.
I don't know where they come from, but something like:
WHERE posts.forumid='$forumid' and posts.type='post'
ORDER BY posts.`timee` DESC
Assuming all come from posts.
you need an alias for this to work
SELECT
posts.*, u1.photo, u1.displayname, u2.photo, u2.displayname FROM posts
JOIN users u1 ON posts.useraid = u1.id
JOIN users u2 ON posts.lastreply = u2.id
WHERE forumid='$forumid' and type='post' ORDER BY `timee` DESC
SELECT posts.*, author.photo as author_photo, author.displayname as author+name,
replier.photo as replier_photo, replier.displayname as replier_name
FROM posts
JOIN users author ON(posts.useraid = users.id)
JOIN users replier ON(posts.lastreply = user.id)
WHERE forumid='$forumid' and type='post' ORDER BY `timee` DESC

Query Issue - LEFT JOINS where specific col is match to variable

I`m fetching data from sql with left join query, I want to filter the query to be more specific,
In my database i have customers, every customer have a Group, every user have his customers. i guess you understand now what i need.
every user will see only the rows that relate to him. this is my query:
SELECT t.*,c.fname,e.DISCODE,e.AREA,e.COLOR
FROM $tbl_name AS t
LEFT JOIN customers AS c ON t.MCcode = c.MCcode
LEFT JOIN eventcodes AS e ON t.MCcode = e.MCcode AND t.CODE=e.CODE
ORDER By `id` DESC LIMIT $start, $limit
ON customers table i have field named Group, and when user is connected i have his Group name, so how i can filter this query that only where ( for example ) c.Group = '$Group', all the rows are found in $tbl_name, and the other details i`m fetching by joins.
thanks!
Like this
LEFT JOIN customers AS c ON t.MCcode = c.MCcode and c.Group = '$Group'

mySQL list profiles and movies using complex joins in one query but three tables

Overview:
I want to get a list of profiles and the movies they like. All the output to be shown on one page. I am using mySQL and PHP, at moment running on localhost. Ideally i want this to be in one query, for speed.
Database table overview in mySQL as follow:
profile_tbl:
> id
> username
> about me
> gender
> hobbies
> profile_pic
> last_login
movies_tbl:
> id
> movie_name
> genre
> length
> rating
> description
profile_movies_rel_tbl
> id
> movie_id
> profile_id
Output:
I want to show 10 profiles and list of the all the movies they like. I was thinking following query:
SELECT profile_tbl.*, movies_tbl.* FROM profile_tbl
LEFT JOIN profile_movies_rel_tbl
ON profile_movies_rel_tbl.movie_id = movies_tbl.id
LEFT JOIN profile_tbl
ON profile_tbl.id= profile_movies_rel_tbl.profile_id LIMIT 10
I also have a second issue, where I want to lists all the profile that have selected movie has favorite, again the page should list profiles and movies together.
In this case, i was thinking of using the above query and adding following:
WHERE profile_movies_rel_tbl.movie_id = 4
Any one need more info, please leave comment.
thanks
I think you want to use LIMIT in a subquery to return 10 distinct profiles:
SELECT profile_tbl.*, movies_tbl.*
FROM (
SELECT DISTINCT Id FROM profile_tbl LIMIT 10
) LimitTable
INNER JOIN profile_tbl ON profile_tbl.Id=LimitTable.Id
LEFT JOIN profile_movies_rel_tbl
ON profile_movies_rel_tbl.profile_id = profile_tbl.id
LEFT JOIN movies_tbl
ON profile_movies_rel_tbl.movie_id = movies_tbl.id
This could return more than 10 rows, but it should only contain 10 profiles.
If you are wanting to return 10 rows, then look into using GROUP_CONCAT to combine your movies into a single column.
You can simple use joins for this. And using GROUP_CONCAT will
give you a list of all movies but comma seperated. Which you can explode
using php explode function whichi will give you the result in an array.
Than you can use loop that array for displaying movie names
SELECT
pt.*,
GROUP_CONCAT(movie_name) AS `Movies`
FROM profile_tbl AS pt
INNER JOIN profile_movies_rel_tbl AS pmrt ON pmrt.profile_id = pt.id
INNER JOIN movies_tbl AS mt ON mt.id = pmrt.movie_id
GROUP BY pt.id
LIMIT 10
ORDER BY pt.id DESC

Categories