MYSQL Distinct results from a query - mysql

What i'm trying to do is get the DISTINCT results using a MYSQL query:
$sql = "SELECT un.*, co.order_product_quantity AS product_num, cu.first_name, cu.surname, cp.product_name, cp.product_thumbnail AS prod_img, co.order_goods_cost, co.order_delivery_cost, co.order_status as status
FROM (
SELECT DISTINCT order_unique_id AS o_id, order_date
FROM cbs_orders
GROUP BY order_unique_id
) un
INNER JOIN cbs_orders co ON co.order_unique_id = un.o_id
INNER JOIN cbs_users cu ON cu.id = co.order_user_id
INNER JOIN cbs_products cp ON cp.product_id = co.order_product_id
ORDER BY un.order_date DESC LIMIT $start_from, $num_rec_per_page ";
I'm looking to get the DISTINCT order_unique_id from the query, i never wrote the query initially, i thought adding "SELECT DISTINCT order_unique_id" would solve the issue but it still brings back duplicate ids.
cheers for any help guys
Graham

You want to leverage GROUP BY correctly; DISTINCT won't help you in this situation. Additional, I'm pretty sure that sub-select is not needed.
Try this
$sql = "SELECT un.order_unique_id AS o_id, un.order_date,
co.order_product_quantity AS product_num,
cu.first_name, cu.surname, cp.product_name,
cp.product_thumbnail AS prod_img, co.order_goods_cost,
co.order_delivery_cost, co.order_status as status
FROM cbs_orders AS un
INNER JOIN cbs_orders co ON co.order_unique_id = un.o_id
INNER JOIN cbs_users cu ON cu.id = co.order_user_id
INNER JOIN cbs_products cp ON cp.product_id = co.order_product_id
GROUP BY un.order_unique_id
ORDER BY un.order_date DESC
LIMIT $start_from, $num_rec_per_page ";
This might produce odd results, because you are not applying aggregate functions to other columns. You may need to use those to get desired results.

Let me hazard a guess that you want records for only the most recent order. If this is the case, then you can do what you want, by properly aggregating the data:
SELECT un.*, co.order_product_quantity AS product_num, cu.first_name,
cu.surname, cp.product_name, cp.product_thumbnail AS prod_img,
co.order_goods_cost, co.order_delivery_cost, co.order_status as status
FROM (SELECT order_unique_id AS o_id, max(order_date) as order_date
FROM cbs_orders
GROUP BY order_unique_id
) un INNER JOIN
cbs_orders co
ON co.order_unique_id = un.o_id INNER JOIN
cbs_users cu
ON cu.id = co.order_user_id INNER JOIN
cbs_products cp
ON cp.product_id = co.order_product_id
ORDER BY un.order_date DESC
LIMIT $start_from, $num_rec_per_page;
If you want only one row per order, then you need to figure out what to do with the products. In my opinion, this interpretation is so different from your current question that you should ask another question, providing sample data and desired results.

Related

SQL - order by is breaking my query when there is no reviews

I have the rather lengthy SQL query that I have included below. As you can see it orders by AvgRating and NumReviews, both of which rely on data from the reviews table. Unfortunately I need to see the rows in my results even when there are no reviews, currently if there are no reviews to order by then that row just doesnt show up in the results. All help greatly appreciated.
SELECT travisor_tradesperson.name, travisor_tradesperson.id, travisor_catagory.catname,
travisor_company.cname, travisor_company.description, travisor_company.city, travisor_company.address, travisor_company.postcode, travisor_company.phone,
ROUND(AVG(travisor_review.rating)) as RoundAvgRating, AVG(travisor_review.rating) as AvgRating, COUNT(travisor_review.rating) as NumReviews
FROM `travisor_tradesperson`
INNER JOIN travisor_company
ON travisor_tradesperson.company = travisor_company.id
INNER JOIN travisor_catagory
ON travisor_tradesperson.catagory = travisor_catagory.id
INNER JOIN travisor_review
ON travisor_review.tradesperson = travisor_tradesperson.id
WHERE travisor_catagory.catname = '$catagory'
AND travisor_company.city = '$city'
GROUP BY travisor_tradesperson.name, travisor_catagory.catname, travisor_company.cname,
travisor_company.description
ORDER BY AvgRating DESC, NumReviews DESC
Left join travisor_review instead of Inner Join. Inner join will only find records that are present in both tables. If you have no reviews for that tradesperson record, it will drop from the results set.
Left join will return a NULL if it cannot match on the join predicate. In this case, the tradesperson will return but with a NULL. Convert the NULL to a 0 if needed and that should fix your AVG.

MAX() Function not working as expected

I've created sqlfiddle to try and get my head around this http://sqlfiddle.com/#!2/21e72/1
In the query, I have put a max() on the compiled_date column but the recommendation column is still coming through incorrect - I'm assuming that a select statement will need to be inserted on line 3 somehow?
I've tried the examples provided by the commenters below but I think I just need to understand this from a basic query to begin with.
As others have pointed out, the issue is that some of the select columns are neither aggregated nor used in the group by clause. Most DBMSs won't allow this at all, but MySQL is a little relaxed on some of the standards...
So, you need to first find the max(compiled_date) for each case, then find the recommendation that goes with it.
select r.case_number, r.compiled_date, r.recommendation
from reporting r
join (
SELECT case_number, max(compiled_date) as lastDate
from reporting
group by case_number
) s on r.case_number=s.case_number
and r.compiled_date=s.lastDate
Thank you for providing sqlFiddle. But only reporting data is given. we highly appreciate if you give us sample data of whole tables.
Anyway, Could you try this?
SELECT
`case`.number,
staff.staff_name AS ``case` owner`,
client.client_name,
`case`.address,
x.mx_date,
report.recommendation
FROM
`case` INNER JOIN (
SELECT case_number, MAX(compiled_date) as mx_date
FROM report
GROUP BY case_number
) x ON x.case_number = `case`.number
INNER JOIN report ON x.case_number = report.case_number AND report.compiled_date = x.mx_date
INNER JOIN client ON `case`.client_number = client.client_number
INNER JOIN staff ON `case`.staff_number = staff.staff_number
WHERE
`case`.active = 1
AND staff.staff_name = 'bob'
ORDER BY
`case`.number ASC;
Check below query:
SELECT c.number, s.staff_name AS `case owner`, cl.client_name,
c.address, MAX(r.compiled_date), r.recommendation
FROM case c
INNER JOIN (SELECT r.case_number, r.compiled_date, r.recommendation
FROM report r ORDER BY r.case_number, r.compiled_date DESC
) r ON r.case_number = c.number
INNER JOIN client cl ON c.client_number = cl.client_number
INNER JOIN staff s ON c.staff_number = s.staff_number
WHERE c.active = 1 AND s.staff_name = 'bob'
GROUP BY c.number
ORDER BY c.number ASC
SELECT
case.number,
staff.staff_name AS `case owner`,
client.client_name,
case.address,
(select MAX(compiled_date)from report where case_number=case.number),
report.recommendation
FROM
case
INNER JOIN report ON report.case_number = case.number
INNER JOIN client ON case.client_number = client.client_number
INNER JOIN staff ON case.staff_number = staff.staff_number
WHERE
case.active = 1 AND
staff.staff_name = 'bob'
GROUP BY
case.number
ORDER BY
case.number ASC
try this

ORDER BY then GROUP BY a datetime value

I have the following MySQL query:
SELECT t.date_time, COUNT(t.submission_id) AS click_count, s.title, s.first_name, s.family_name, s.email, ut.ukip_name, a.advertiser_name
FROM recards.tracking t
INNER JOIN submissions s ON t.submission_id = s.submission_id
INNER JOIN form_settings fs ON t.form_id = fs.form_id
INNER JOIN advertisers a ON t.advertiser_id = a.advertiser_id
INNER JOIN db2829.ukip_titles ut ON fs.ukip_title_id = ut.ukip_title_id
WHERE t.advertiser_id = 621
GROUP BY t.submission_id
ORDER BY t.date_time DESC
However, of course, as the ORDER BY is done after the GROUP BY, I only get the first record, where as I need the last. Investigating, I read about using MAX() on the column during the SELECT, but that does not work with datetime types.
Is there another work around? Maybe embedding SELECTs within SELECTs? What I have above is about as far as my MySQL knowledge goes, unfortunately!
Turns out I was just forgetting the AS after the MAX(), with MAX(t.date_time) AS top_time it works fine.
My full query working:
SELECT MAX(t.date_time) AS top_time, COUNT(t.submission_id) AS click_count, s.title, s.first_name, s.family_name, s.email, ut.ukip_name, a.advertiser_name
FROM recards.tracking t
INNER JOIN submissions s ON t.submission_id = s.submission_id
INNER JOIN form_settings fs ON t.form_id = fs.form_id
INNER JOIN advertisers a ON t.advertiser_id = a.advertiser_id
INNER JOIN db2829.ukip_titles ut ON fs.ukip_title_id = ut.ukip_title_id
WHERE t.advertiser_id = 621
GROUP BY t.submission_id
ORDER BY top_time DESC
You are just grouping by t.submission_id and nothing else, so this query will not work. The other attributes need to be added to the GROUP BY or need to be eliminated from the SELECT.
As far as ordering add MAX(t.date_time) or MIN(t.date_time) to your SELECT and then ORDER BY that result either ascending or descending.

MYSQL Inner join for three to four tables

Please I need to figure out what I am doing wrong. I created this inner join code for mysql. it works but it gives me repeated values like repeating a particular row twice or categoryid twice. each of the tables(users,paymentnotification,monthlyreturns) has the categoryid used to check and display the username(users.pname) from the user table, then check and display those that have made payment from the monthly returns and payment table using the categoryid.
$r="SELECT monthlyreturns.categoryid, monthlyreturns.month, monthlyreturns.quarter, monthlyreturns.year,paymentnotification.amount, users.pname, monthlyreturns.ototal, paymentnotification.payee, status
FROM paymentnotification
INNER JOIN (monthlyreturns INNER JOIN users ON monthlyreturns.categoryid=users.categoryid)
ON monthlyreturns.categoryid=paymentnotification.categoryid
ORDER BY monthlyreturns.categoryid DESC";
I think the query you want is more like this:
SELECT b.categoryid, b.month, b.quarter, b.year, a.amount, c.pname, b.ototal, a.payee, status
FROM paymentnotification a
INNER JOIN monthlyreturns b
ON a.categoryid = b.categoryid
INNER JOIN users c
ON b.categoryid = c.categoryid
ORDER BY b.categoryid DESC
The way you are doing the correlations doesn't seem clear and may cause problems. Try this one out and see what happens. If its still doing duplicates, perhaps the nature of the data require further filtering.
Assuming I understand what you're trying to do, you are not joining your tables properly. Try joining one at a time
SELECT DISTINCT monthlyreturns.categoryid, monthlyreturns.month, monthlyreturns.quarter, monthlyreturns.year,paym entnotification.amount, users.pname, monthlyreturns.ototal, paymentnotification.payee, status
FROM paymentnotification
INNER JOIN monthlyreturns
ON paymentnotification.categoryid = monthlyreturns.categoryid
INNER JOIN users
ON monthlyreturns.categoryid = users.categoryid
ORDER BY monthlyreturns.categoryid DESC
I don't see any problem.. I get 4 result rows: check this fiddle http://sqlfiddle.com/#!2/165a22/5
this is the query I used:
SELECT m.categoryid, m.month, m.quarter, m.year,p.amount, u.pname, m.ototal, p.payee, m.status
FROM paymentnotification p JOIN monthlyreturns m ON p.categoryid = m.categoryid
JOIN users u ON u.categoryid = m.categoryid
ORDER BY m.categoryid DESC
there are no duplicated rows, just "unique" rows if you consider every column you choose.
Hope it helps
SELECT M.categoryid, M.month, M.quarter, M.year, M.ototal,
P.amount, P.payee, P.status,
U.pname
FROM paymentnotification AS P
INNER JOIN monthlyreturns AS M ON P.categoryid = M.categoryid
INNER JOIN users AS U ON M.categoryid = U.categoryid
ORDER BY M.categoryid DESC

Multiple Joins Mysql Doubles SUM values

Im trying to make a a query, but its doubling the Sum values
SELECT
cidades.id AS id,
cidades.name AS municipio,
Sum(conjuntos.n_uhs) AS uh,
programas.id AS programa,
conjuntos.name
FROM
conjuntos
Inner Join conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
Inner Join programas ON programas.id = conjuntos_programas.programa_id
Inner Join cidades ON conjuntos.cidade_id = cidades.id
WHERE
conjuntos.situation_id = 2
GROUP BY
conjuntos.cidade_id
ORDER BY
municipio ASC
You've got duplicate rows, you can check this by removing the group by and the SUM(... from your query.
Change the query as follows and tell me if that fixes to problem.
SELECT DISTINCT
cidades.id AS id,
cidades.name AS municipio,
SUM(conjuntos.n_uhs) AS uh,
programas.id AS programa,
conjuntos.name
FROM conjuntos
INNER JOIN conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
INNER JOIN programas ON programas.id = conjuntos_programas.programa_id
INNER JOIN cidades ON conjuntos.cidade_id = cidades.id
WHERE conjuntos.situation_id = 2
GROUP BY conjuntos.cidade_id
ORDER BY municipio ASC
It sounds like you have a one to many relationship between two or more of your tables to be doing this. Try doing a SELECT * and start debugging your query to see where it is duplicating the rows.