MYSQL Order from another Table - php

I have a question.
I have here 2 tables
table 1 : products
product_id , name , images_sideview
table 2 : product_descriptions
product_id , volgnr
I want to sort them with the numbers from Table 2 and volgnr field.
Is there a way to do this with mysql ?

There are 2 ways to sort. Ascending order and Descending order. You have not mentioned the order. So I am providing you both answers with 2 variations:
ASCENDING ORDER:
SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.product_id ASC, table2.volgnr ASC;
DESCENDING ORDER:
SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.product_id DESC, table2.volgnr DESC;
If you want to tell MySQL to first sort FIRST by volgnr and then by product_id:
ASCENDING ORDER:
SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.volgnr ASC, table2.product_id ASC;
DESCENDING ORDER:
SELECT DISTINCT table1.*
FROM table1
INNER JOIN table2 ON table1.product_id = table2.product_id
GROUP BY table1.product_id
ORDER BY table2.volgnr DESC, table2.product_id DESC;
Hope that helps.
Edit 1:
I have now edited the query so that it does not give you duplicates in results. Try it out and let me know how that goes.
Edit 2:
Added Group By clause. Try this out.

select *
from products t1
inner join product_descriptions t2 on t1.product_id = t2.product_id
order by t2.volgnr

SELECT p.'product_id', p.'name', p.'images_sideview'
FROM products p
LEFT JOIN product_descriptions d ON p.product_id = d.product_id
ORDER BY d.volgnr;

Related

MySQL two-table INNER JOIN , LEFT JOINED onto third table with only one row with lowest value

I searched around and found a near example to what I'm looking for, but it doesn't work in my case.
I have a query that does an INNER JOIN on two tables and this join constrains my overall data set substantially. I then want to LEFT JOIN onto a third table but I only want one record from that third table. The reason for the left join is because not every result of the INNER JOIN has a match in the 3rd table. Something like this:
SELECT DISTINCT t1.code, t2.id, t2.code, t3.id, t3.source_title, t3.display_order
FROM table1 t1
INNER JOIN table2 t2 ON t2.code=t1.code AND t2.type=0
LEFT JOIN table3 t3 ON t3.code=t1.code
ORDER BY t1.code, t3.display_order
This query returns too many records because the third table contains multiple records with a matching code. I just want the first one that matches with the lowest display_order value and, unfortunately, I can't limit the records to have display_order=1 because the lowest display order is not always one.
IMPORTANT: The t3.id value (if any) returned by this query must correspond to the record with the lowest display_order value. I.e., it won't work if the query correctly returns the lowest display_order value but the t3.id value corresponds to some other record in table 3.
Is this even possible? Any help would be much appreciated.
EDIT: Per Nick's suggestion, I have tried this, which appears to be working. I'll do some verification and report back:
SELECT DISTINCT t1.code, t2.*, sq.id, sq.source_title, sq.display_order
FROM table1 t1
INNER JOIN table2 p ON t2.code=t1.code AND t2.type=0
LEFT JOIN (
SELECT t3.*
FROM table3 t3
WHERE t3.display_order=(
SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code
)
) sq ON sq.code=t1.code
ORDER BY t1.code, sq.display_order
You should be able to replace table3 in your LEFT JOIN with
(SELECT *
FROM table3 t3
WHERE display_order = (SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code)
) t3
In MySQL 8.0 you can try to use row_number() for each code and ordered by display_order in a subquery from table3. Then left join that result and check for the row_number() to be equal to 1.
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
t3.id,
t3.source_title,
t3.display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
LEFT JOIN (SELECT t3.id,
t3.source_title,
t3.display_order,
t3.code,
row_number() OVER (PARTITION BY t3.code
ORDER BY t3.display_order) rn
FROM table3 t3) t3
ON t3.code = t1.code
WHERE t2.type = 0
AND t3.rn = 1
ORDER BY t1.code,
t3.display_order;
In lower versions you can try correlated subqueries ordered by display_order and LIMIT 1 (to get only one record).
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
(SELECT t3.id
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) id,
(SELECT t3.source_title
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) source_title,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
WHERE t2.type = 0
ORDER BY t1.code,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1);
I assumed, that display_order in table3 isn't unique but id is. So I added id to the ORDER BY clauses in the subqueries to make sure the same record is selected in each of them. If display_order is unique, you can remove id FROM the ORDER BY clauses.
Edit:
If you don't want to repeat the subqueries in the (overall) ORDER BY clause, you can also order by the column ordinals. E.g.:
...
ORDER BY 1, 6;

PHP Mysql Joins and Where Clause

I need help on below statement. I need to put WHERE items.IID = 8 so that it shows only details pertaining to IID number 8. But when I use WHERE items.IID = 8, it is not working. I have to use this type of join as I want to do Sum and Count of some fields. There are 3 tables. 1st is items, 2nd is ItemPurchaseHistory and 3rd is ItemIssuedHistory.
SELECT items.IID, items.ItemName, ItemPurchaseHistorySum.SumOfUnitsPurchased, ItemPurchaseHistorySum.SumOfCost,
ItemPurchaseHistoryCount.CountOfUnitsPurchased,
ItemIssuedHistorySum.SumOfUnitsIssued
FROM items
LEFT JOIN (SELECT IID, SUM(UnitsPurchased) AS SumOfUnitsPurchased, SUM(Cost) AS SumOfCost
FROM ItemPurchaseHistory
GROUP BY IID) ItemPurchaseHistorySum ON ItemPurchaseHistorySum.IID = items.IID
LEFT JOIN (SELECT IID, Count(UnitsPurchased) AS CountOfUnitsPurchased
FROM ItemPurchaseHistory
GROUP BY IID) ItemPurchaseHistoryCount ON ItemPurchaseHistoryCount.IID = items.IID
LEFT JOIN (SELECT IID, SUM(UnitsIssued) AS SumOfUnitsIssued
FROM ItemIssuedHistory
GROUP BY IID) ItemIssuedHistorySum ON ItemIssuedHistorySum.IID = items.IID
WHERE item.IID = $_GET['id']
ORDER BY items.IID ASC

MySQL left join count with condition

I have two tables:
cc_videos with fields: id, challenge_id, upload_date, owner_id
cc_video_votes with fields id, video_id, vote_date
Field "id" in "cc_videos" corresponds to a field "video_id" in "cc_video_votes" and I have a statement like this:
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
GROUP BY cc_videos.id
ORDER BY votes_count desc
Now this works OK - lists ALL VIDEOS sorted by videos with highest vote rate first. Now i want to put condition to list only videos from "cc_videos" WHERE challenge_id matches variable "$challenge_id", but when I put condition like this it returns 0 results:
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos WHERE challenge_id = "$challenge_id"
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
GROUP BY cc_videos.id
ORDER BY votes_count desc
Am I using WHERE clause correctly ?
No you are not.
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
WHERE cc_videos.challenge_id = "$challenge_id"
GROUP BY cc_videos.id
ORDER BY votes_count desc
WHERE syntax should stay in clauses, check documentation here
Change your query to:
SELECT cc_videos.*,
Count(video_id) AS votes_count
FROM cc_videos
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
WHERE cc_videos.challenge_id = "$challenge_id"
GROUP BY cc_videos.id
ORDER BY votes_count DESC

SELECT LEFT JOIN with LIMIT

I have a problem putting a limit on the number of rows from my Jokes table.
This is my query working, getting all rows:
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Would it be something like?
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM (
SELECT * FROM Jokes ORDER BY ID DESC Limit 0,40)
AS a
LEFT JOIN Categories
AS b
ON a.CategoryID = b.ID
why not using
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Limit 0,40

MySQL query order the results in GROUP BY 3 Tables Count

I'm coding a listing system and I'm trying to get the posts ORDER by number of comments and votes FROM 2 tables.
Table1 : Lists => id, title, detail
Table2 : Votes => voteid, listid
Table3 : Comments => commentid, listid
WHERE MY Current query is
$q = mysql_query("SELECT * FROM zoo_leads
LEFT JOIN Votes ON Lists.id=Votes.listid
LEFT JOIN Comments ON Lists.id=Comments.listid
GROUP BY Lists.id ORDER BY Comments.listid DESC LIMIT 10
it is showing me results perfectly but ORDER BY is Lists.id Instead of number of votes and comments
Try:
SELECT *
FROM zoo_leads
LEFT JOIN votes
ON lists.id = votes.listid
LEFT JOIN comments
ON lists.id = comments.listid
GROUP BY lists.id
ORDER BY COUNT(votes.id) DESC,
COUNT(comments.id) DESC
LIMIT 10
That is because you have ORDER BY Comments.listid in your SQL statement.

Categories