I keep running on an issue in calling from MySQL and getting what I need. I have two tables called projects and entries, what I am trying to do is get the latest time stamped entry. The SQL query is as follows:
SELECT
projects.ProjectLogo, projects.ProjectLink, projects.ProjectDescription,
entries.EntryNum, entries.Votes, entries.Views, entries.Update
FROM
projects
LEFT JOIN entries
ON projects.ProjectID = entries.ProjectID
AND projects.Media = 'image'
AND projects.Type = 'fan-art'
GROUP BY
projects.ProjectID
ORDER BY
entries.Update DESC
The issue is that I get the results but not the latest entry, I have used MAX(entries.Update) but it does not work. Any suggestions? Why does it not work?
You can use a subquery to get the latest Update for every ProjectID on table entries. The result of the subquery is then join back on the two join statements providied that it will match on two columns: ProjectID and Update.
SELECT projects.ProjectLogo,
projects.ProjectLink,
projects.ProjectDescription,
entries.EntryNum,
entries.Votes,
entries.Views,
entries.Update
FROM projects
INNER JOIN entries
ON projects.ProjectID = entries.ProjectID
INNER JOIN
(
SELECT a.ProjectID, MAX(a.Update) max_val
FROM entries a
GROUP BY a.ProjectID
) b ON b.ProjectID = entries.ProjectID AND
b.max_val = entries.Update
WHERE projects.Media = 'image' AND
projects.Type = 'fan-art'
ORDER BY entries.Update DESC
Try using a subquery to get the latest entry:
select projectId, max(`update`) as lastUpdate
from entries
group by projectId
And now, use this subquery to get what you need:
select ...
from
projects as p
inner join entries as e on p.projectId=e.projectId
inner join (
select projectId, max(`update`) as lastUpdate
from entries
group by projectId) as me on e.`update`=me.lastUpdate
Hope this helps you
You have to restrict the join to only the record (row) with the latest entry date.
if the pk in entries is chronologically increasing, you can use it.
Select p.ProjectLogo, p.ProjectLink, p.ProjectDescription,
e.EntryNum, e.Votes, e.Views, e.Update
From projects p
Left Join entries e
On e.EntryId =
(Select(Max(entryId) from entries
where ProjectID = p.ProjectID)
Where p.Media = 'image'
And p.Type = 'fan-art'
Group By p.ProjectID
Order By e.Update Desc
Otherwise, you need a double nested subquery
Select p.ProjectLogo, p.ProjectLink, p.ProjectDescription,
e.EntryNum, e.Votes, e.Views, e.Update
From projects p
Left Join entries e
On e.EntryId =
(Select entryId from entries
where ProjectID = p.ProjectID
And update =
(Select max(update) From entries
Where ProjectID = p.ProjectID))
where p.Media = 'image'
And p.Type = 'fan-art'
Group By p.ProjectID
Order By e.Update Desc
Related
I am running a query on a PHP page that will pull all records from one table, INNER JOIN with two other tables and then list all of the results. However on the second table I only want the most recent record.
Here is my query
SELECT * FROM wn_trailer
INNER JOIN (
SELECT id, trailer_id, trailer_status, trailer_assigned, MAX(last_update), trailer_lat, trailer_long
FROM wn_trailer_history
) AS th ON wn_trailer.id = th.trailer_id
INNER JOIN wn_trailer_status ON wn_trailer_status.id = th.trailer_status
INNER JOIN wn_users ON wn_users.id = th.trailer_assigned
ORDER BY trailer_number ASC
The query runs but returns only the first record.
You want an additional JOIN to bring in the data on the last update date. Also, your subquery needs a GROUP BY:
SELECT *
FROM wn_trailer t INNER JOIN
(SELECT trailer_id, MAX(last_update) as max_last_update
FROM wn_trailer_history
GROUP BY trailer_id
) tht
ON t.id = tht.trailer_id INNER JOIN
wn_trailer_history th
ON th.trailer_id = tht.trailer_id AND
th.last_update = tht.max_last_update INNER JOIN
wn_trailer_status ts
ON ts.id = th.trailer_status INNER JOIN
wn_users u
ON u.id = th.trailer_assigned
ORDER BY trailer_number ASC;
I also added table aliases so the query is easier to write and to read.
I have the following MySQL query:
SELECT *
FROM person person
LEFT OUTER JOIN (employee_location employee_location
INNER JOIN location_status location_status
ON employee_location.type = location_status.status_id )
ON person.per_id = employee_location.person_id
ORDER BY person.per_given
Which gives me the following result:
I want to be able to show only the latest record for each person:
Am I able to do this just by adding something to the MySQL query above?
My schema:
Table = Columns
person = per_id, per_given
employee_location = id, person_id, type, date_time
location_status = status_id, status_type
One method is a correlated subquery. I can speculate on what your tables look like, so I'm guessing the query looks like this:
SELECT *
FROM person p JOIN
employee_location el
ON el.person_id = p.per_id JOIN
location_status ls
ON el.type = ls.status_id
WHERE el.date_time = (SELECT MAX(el2.date_time)
FROM employee_location el2
WHERE el2.per_id = el.per_id
)
ORDER BY p.per_given
You could use MAX():
SELECT per_id, MAX(date_time) FROM person GROUP BY per_id;
I want to get all the data from the users table & the last record associated with him from my connection_history table , it's working only when i don't add at the end of my query
ORDER BY contributions DESC
( When i add it , i have only the record wich come from users and not the last connection_history record)
My question is : how i can get the entires data ordered by contributions DESC
SELECT * FROM users LEFT JOIN connections_history ch ON users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date)
The order by should not affect the results that are returned. It only changes the ordering. You are probably getting what you want, just in an unexpected order. For instance, your query interface might be returning a fixed number of rows. Changing the order of the rows could make it look like the result set is different.
I will say that I find = to be more intuitive than EXISTS for this purpose:
SELECT *
FROM users u LEFT JOIN
connections_history ch
ON u.id = ch.guid AND
ch.date = (SELECT Max(ch1.date)
FROM connections_history ch1
WHERE ch.guid = ch1.guid
)
ORDER BY contributions DESC;
The reason is that the = is directly in the ON clause, so it is clear what the relationship between the tables is.
For your casual consideration, a different formatting of the original code. Note in particular the indented AND suggests the clause is part of the LEFT JOIN, which it is.
SELECT * FROM users
LEFT JOIN connections_history ch ON
users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date
)
We can use nested queries to first check for max_date for a given user and pass the list of guid to the nested query assuming all the users has at least one record in the connection history table otherwise you could use Left Join instead.
select B.*,X.* from users B JOIN (
select A.* from connection_history A
where A.guid = B.guid and A.date = (
select max(date) from connection_history where guid = B.guid) )X on
X.guid = B.guid
order by B.contributions DESC;
I have a table with deals data written in it. I need to query said deals while joining on other tables, however d.originalorderid is not a unique entry and I get several duplicate entries. I want to:
For each unique d.originalorderid select one row
This row should be most recent (largest ID)
How would I go about this? This is the query I have right now.
SELECT d.id,
d.date,
d.ip, d.panmask,
d.merchantorderid,
d.amount,
d.cardholder,
d.bankhumanname,
d.cardtypeid,
d.bankcountrycode,
d.usercountrycode,
mc.paymentkey as merchantname,
dt.status,
d.merchantcontract,
dt.tag,
d.originalorderid,
ds.refnumber,
ds.dealauthcode,
mc.processingid,
pc.Name as processing,
d.customparams
FROM Deal as d
LEFT JOIN MerchantContract as mc ON mc.Id = d.MerchantContract
LEFT JOIN DealTrace as dt ON d.Id = dt.DealId
AND dt.id = (SELECT MAX(id)
FROM DealTrace WITH (nolock)
WHERE DealId = d.id)
LEFT JOIN DealSummary ds ON d.Id = ds.DealId
AND ds.id = (SELECT MAX(id)
FROM DealSummary WITH (nolock)
WHERE DealId = d.id)
LEFT JOIN Processing pc on mc.ProcessingId = pc.id
WHERE (d.MerchantContract IN ('12'))
ORDER BY ID desc OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY
If I understand the requirement, instead of joining to the Deal table, join to a correlated subquery on it which returns the deal with the highest deal id which has the same original order id. Test it but I think its OK..
SELECT ....
FROM
(SELECT *
FROM Deal d1
WHERE d1.Id=(SELECT MAX(Id)
FROM Deal d2
WHERE d2.OriginalOrderId=d1.OriginalOrderId)) d
LEFT JOIN MerchantContract as mc ON mc.Id = d.MerchantContract
etc etc
when i write a MySQL query, there occur a problem. here is my query
SELECT
SUM(view_product_count_details.view_product_count) AS count_sum,
product_details.product_name,
product_details.product_url,
product_details.product_price,
product_image_details.product_image_name,
main_category_details.main_category_url,
sub_category_details.sub_category_url
FROM
view_product_count_details
JOIN
product_details ON view_product_count_details.product_id_fk = product_details.product_id
JOIN
product_image_details ON product_image_details.product_id_fk = view_product_count_details.product_id_fk
JOIN
main_category_details ON product_details.product_main_cat_id = main_category_details.main_category_id
JOIN
sub_category_details ON product_details.product_sub_cat_id_fk = sub_category_details.sub_category_id
WHERE
view_product_count_details.view_product_status = 'active'
GROUP BY view_product_count_details.product_id_fk
ORDER BY count_sum DESC
LIMIT 4
Here I have multiple images for one product.the images are in table "product_image_details". this query returns count as the number of images, where I need the count of product viewed by people which is stored in table "view_product_count_details". when I just pick the count, i got the count as it is. but when i join the table "product_image details", result become wrong. Is there any way to do it in single query?
Please help me... Thanks in advance.... :)
You can do it by having an inline query. I am not sure how this will perform when you have more data.
SELECT table1.*,product_image_details.product_image_name FROM
(
SELECT
SUM(view_product_count_details.view_product_count) AS count_sum,
product_details.product_id,
product_details.product_name,
product_details.product_url,
product_details.product_price,
main_category_details.main_category_url,
sub_category_details.sub_category_url
FROM
view_product_count_details
JOIN
product_details ON view_product_count_details.product_id_fk = product_details.product_id
JOIN
product_image_details ON product_image_details.product_id_fk = view_product_count_details.product_id_fk
JOIN
main_category_details ON product_details.product_main_cat_id = main_category_details.main_category_id
JOIN
sub_category_details ON product_details.product_sub_cat_id_fk = sub_category_details.sub_category_id
WHERE
view_product_count_details.view_product_status = 'active'
GROUP BY view_product_count_details.product_id_fk
ORDER BY count_sum DESC
LIMIT 4
) table1
JOIN
product_image_details ON product_image_details.product_id_fk = table1.product_id
LIMIT 4