Column 'task_id' in order clause is ambiguous - mysql

SELECT a.*, p.*, t.*, r.*, n.*
FROM prop_assigns AS a
LEFT JOIN properties AS p
ON (p.property_id = a.property_id)
LEFT JOIN tasks AS t ON (t.task_id = a.task_id)
LEFT JOIN reminders_tasks AS r ON (a.assign_id = r.assign_id)
LEFT JOIN notes_view AS n ON (p.property_id = n.property_id)
WHERE a.user_id = 3 AND a.task_id <> 0 AND
a.assign_done = 0 AND n.user_id = 3 ORDER BY task_id desc
MySQL a répondu: Documentation
#1052 - Column 'task_id' in order clause is ambiguous
it look long sql any help

The field task_id is in more than one table. You need to specify which you mean:
SELECT a.*, p.*, t.*, r.*, n.*
FROM prop_assigns AS a LEFT JOIN
properties AS p
ON (p.property_id = a.property_id) LEFT JOIN
tasks AS t
ON (t.task_id = a.task_id) LEFT JOIN
reminders_tasks AS r
ON (a.assign_id = r.assign_id) LEFT JOIN
notes_view AS n
ON (p.property_id = n.property_id)
WHERE a.user_id = 3 AND a.task_id <> 0 AND a.assign_done = 0 AND n.user_id = 3
ORDER BY t.task_id desc;
---------^

Related

Left Join based on field value

I want to get all the detail about my purchase from the following tables,
1) tbl_track
2) tbl_album
3) tbl_purchase
tbl _purchase table :-
item_id
type
price
tbl_track table :-
id
title
description
tbl_album table :-
id
title
description
Now based on type of tbl_purchase I want to join one of table,
It means If type = 0 then LEFT JOIN with tbl_track and for type = 1 then LEFT JOIN with tbl_album.
I want to do these with one query only, It is easily done with two query.
Thanks in advance.
With UNION:
SELECT p.type, t.id, t.title, t.description
FROM tbl_purchase p
JOIN tbl_track t ON t.id = p.item_id AND p.type = 0
UNION
SELECT p.type, a.id, a.title, a.description
FROM tbl_purchase p
JOIN tbl_album a ON a.id = p.item_id AND p.type = 1
I think you need left join both tables in any case. So you can use next query for example
SELECT P.*,
T.id AS track_id,
A.id AS album_id
FROM tbl_purchase AS P
LEFT JOIN tbl_track AS T ON P.item_id = T.id
AND P.type = 0
LEFT JOIN tbl_album AS A ON P.item_id = A.item_id
AND P.type = 1
Try this:
SELECT *
FROM tbl_purchase A
LEFT OUTER JOIN tbl_track B ON A.item_id = B.id AND A.type = 0
LEFT OUTER JOIN tbl_album C ON A.item_id = C.id AND A.type = 1;

Mysql can't get data of my parent in a subquery

I've to do a query where the join can access to the data of is parent.
I had this :
SELECT *
FROM tbl_quart q
INNER JOIN tbl_poste_horaire ph ON ph.tbl_quart_id = q.id
INNER JOIN tbl_poste_horaire_caserne phc ON phc.tbl_poste_horaire_id = ph.id
INNER JOIN (
SELECT DISTINCT q1.id,
IF(q1.heureDebut>CURTIME(),ADDTIME(q1.heureDebut,'24:00:00'),
IF(q1.heureFin<CURTIME(),ADDTIME(q1.heureDebut,'48:00:00'),q1.heureDebut)) AS heureTri
FROM tbl_poste_horaire_caserne phc1
INNER JOIN tbl_poste_horaire ph1 ON ph1.id = phc1.tbl_poste_horaire_id
INNER JOIN tbl_quart q1 ON q1.id = ph1.tbl_quart_id
WHERE phc1.tbl_caserne_id = phc.tbl_caserne_id
ORDER BY phc1.tbl_caserne_id, heureTri
LIMIT 2
) AS quartValide ON quartValide.id = q.id
WHERE phc.tbl_caserne_id IN (1,9)
but this request give me that:
Error Code: 1054
Unknown column 'phc.tbl_caserne_id' in 'where clause'
the error is for this line :
WHERE phc1.tbl_caserne_id = phc.tbl_caserne_id
I had also try this :
SELECT *
FROM tbl_quart q
INNER JOIN tbl_poste_horaire ph ON ph.tbl_quart_id = q.id
INNER JOIN tbl_poste_horaire_caserne phc ON phc.tbl_poste_horaire_id = ph.id
WHERE phc.tbl_caserne_id IN (1,9) AND q.id IN ( SELECT DISTINCT q1.id,
IF(q1.heureDebut>CURTIME(),ADDTIME(q1.heureDebut,'24:00:00'),
IF(q1.heureFin<CURTIME(),ADDTIME(q1.heureDebut,'48:00:00'),q1.heureDebut)) AS heureTri
FROM tbl_poste_horaire_caserne phc1
INNER JOIN tbl_poste_horaire ph1 ON ph1.id = phc1.tbl_poste_horaire_id
INNER JOIN tbl_quart q1 ON q1.id = ph1.tbl_quart_id
WHERE phc1.tbl_caserne_id = phc.tbl_caserne_id
ORDER BY phc1.tbl_caserne_id, heureTri
LIMIT 2
)
And i had an error too:
Error Code: 1235
This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Thanks you for your help :)
Try this way:
SELECT *
FROM tbl_quart q
INNER JOIN tbl_poste_horaire ph ON ph.tbl_quart_id = q.id
INNER JOIN tbl_poste_horaire_caserne phc ON phc.tbl_poste_horaire_id = ph.id
INNER JOIN (
SELECT DISTINCT q1.id,
phc1.tbl_caserne_id
IF(q1.heureDebut>CURTIME(),ADDTIME(q1.heureDebut,'24:00:00'),
IF(q1.heureFin<CURTIME(),ADDTIME(q1.heureDebut,'48:00:00'),q1.heureDebut)) AS heureTri
FROM tbl_poste_horaire_caserne phc1
INNER JOIN tbl_poste_horaire ph1 ON ph1.id = phc1.tbl_poste_horaire_id
INNER JOIN tbl_quart q1 ON q1.id = ph1.tbl_quart_id
ORDER BY phc1.tbl_caserne_id, heureTri
LIMIT 2
) AS quartValide
ON quartValide.id = q.id AND quartValide.tbl_caserne_id = phc.tbl_caserne_id
WHERE phc.tbl_caserne_id IN (1,9)
but it would be better to refactor this query... give me a minute, I think we can simplify it.
it looks like you overcomplecated the query a little. Try this one and tell me what is wrong with result?
SELECT DISTINCT q.*,
ph.*,
phc.*,
IF(q.heureDebut>CURTIME(),ADDTIME(q.heureDebut,'24:00:00'),
IF(q.heureFin<CURTIME(),ADDTIME(q.heureDebut,'48:00:00'),q.heureDebut)) AS heureTri
FROM tbl_poste_horaire_caserne phc
INNER JOIN tbl_poste_horaire ph
ON ph.id = phc.tbl_poste_horaire_id
INNER JOIN tbl_quart q
ON q.id = ph.tbl_quart_id
WHERE phc.tbl_caserne_id IN (1,9)
ORDER BY phc.tbl_caserne_id, heureTri
LIMIT 2

Problems with reusing LEFT JOIN results in WHERE and ORDER BY Clause

SELECT s.*,
u.username,
u.fullname,
c.title AS ctitle,
c.description AS cdescription,
sa.attention,
sp.popularity,
COUNT(DISTINCT f.id) AS favorites,
COUNT(DISTINCT st.id) AS stars,
COUNT(DISTINCT v.id) AS views
FROM shots s
INNER JOIN users u ON u.id = s.user_id
INNER JOIN categories c ON c.id = s.cat_id
LEFT OUTER JOIN(
SELECT shot_id, round(AVG(count),2) AS attention
FROM points
WHERE date > DATE_SUB(CURDATE(),INTERVAL 2 DAY)
GROUP BY shot_id
) sa ON sa.shot_id = s.id
LEFT OUTER JOIN(
SELECT shot_id, SUM(count) AS popularity
FROM points
GROUP BY shot_id
) sp ON sp.shot_id = s.id
LEFT OUTER JOIN favorites f ON f.shot_id = s.id
LEFT OUTER JOIN stars st ON st.shot_id = s.id
LEFT OUTER JOIN views v ON v.shot_id = s.id
**WHERE s.library = 1 AND sa.attention > 40
ORDER BY sa.attention DESC
LIMIT 0,50**
GROUP BY s.id
I can't use the sa.attention in a condition and for ordering. Why?
(I removed the marked part, and the query works!)
What do I have to change in my Query? And if you could give a explanation for it, that would be very nice!
You are negating your OUTER JOIN by putting that in your WHERE criteria. Move it to your JOIN and you'll get your NULL records back:
SELECT s.*,
u.username,
u.fullname,
c.title AS ctitle,
c.description AS cdescription,
sa.attention,
sp.popularity,
COUNT(DISTINCT f.id) AS favorites,
COUNT(DISTINCT st.id) AS stars,
COUNT(DISTINCT v.id) AS views
FROM shots s
INNER JOIN users u ON u.id = s.user_id
INNER JOIN categories c ON c.id = s.cat_id
LEFT OUTER JOIN(
SELECT shot_id, round(AVG(count),2) AS attention
FROM points
WHERE date > DATE_SUB(CURDATE(),INTERVAL 2 DAY)
GROUP BY shot_id
) sa ON sa.shot_id = s.id AND sa.attention > 40
LEFT OUTER JOIN(
SELECT shot_id, SUM(count) AS popularity
FROM points
GROUP BY shot_id
) sp ON sp.shot_id = s.id
LEFT OUTER JOIN favorites f ON f.shot_id = s.id
LEFT OUTER JOIN stars st ON st.shot_id = s.id
LEFT OUTER JOIN views v ON v.shot_id = s.id
WHERE s.library = 1
GROUP BY s.id
ORDER BY sa.attention DESC
LIMIT 0,50
A second note, GROUP BY cannot go at the end. I moved that to the correct spot as well.

Select 2 counts from a join query

This query always returns "num_of_accounts" the same as num_of_users
but if i remove the second join with lists_user_assignment
num_of_accounts is correct
select lists.*, count(lists_account_assignment.id) as num_of_accounts, count(lists_user_assignment.id) as num_of_users
from lists
left join lists_account_assignment on lists.id = lists_account_assignment.lists_id
left join lists_user_assignment on lists.id = lists_user_assignment.lists_id
where lists.tenant_id = 1
group by lists.id
Not sure if this is the right way to get 2 counts in a query. Also, if this query will be expensive or not. Would appreciate help.
Try using a COUNT(DISTINCT ..):-
SELECT lists.*,
Count(DISTINCT lists_account_assignment.id) AS num_of_accounts,
Count(DISTINCT lists_user_assignment.id) AS num_of_users
FROM lists
LEFT JOIN lists_account_assignment
ON lists.id = lists_account_assignment.lists_id
LEFT JOIN lists_user_assignment
ON lists.id = lists_user_assignment.lists_id
WHERE lists.tenant_id = 1
GROUP BY lists.id
try this..
SELECT L.*, count(Acc.id) AS NoOfAccounts
FROM lists L
LEFT JOIN lists_account_assignment Acc
ON L.id = Acc.lists_id
LEFT JOIN lists_user_assignment U
ON L.id = U.lists_id
AND L.tenant_id = 1
GROUP BY L.id

How to access outer table in a LEFT OUTER JOIN

In the query below, I am trying to use the first table in a left outer join. However I am getting an error.
SELECT
products.id,
products_cstm.oem_c,
products.mfr_part_num,
products.description,
products.cost,
products.assigned_user_id,
customfields_oo.ans
FROM products
LEFT OUTER JOIN (SELECT COUNT( q.id ) AS ans
, pq.product_id
FROM products_quotes pq
LEFT JOIN quotes q
ON pq.quote_id = q.id
WHERE q.deleted = 0
AND pq.deleted = 0
AND q.stage <> 4
AND (pq.qty_shipped < pq.product_qty)
AND pq.product_id = products.id
GROUP BY pq.product_id
) AS customfields_oo
ON customfields_oo.product_id = products.id
LEFT JOIN products_cstm
ON products.id = products_cstm.id_c
WHERE products.deleted = 0
ORDER BY ans DESC
When I run the query it gives me the following error:
Error Code : 1054
Unknown column 'products.id' in 'where clause'
It is not allowing first "products" table in left outer join query.
The issue is that customfields_oo is a derived table not a correlated subquery. Thus, you cannot reference the outer table from within the definition of the derived table. In this case, you cannot refer to the outer products table from within the customfields_oo definition. Instead, you must do that filter in the On clause outside the dervied table definition.
Select products.id,
products_cstm.oem_c,
products.mfr_part_num,
products.description,
products.cost,
products.assigned_user_id,
customfields_oo.ans
FROM products
Left Join (
Select pq1.product_id
, Count( q1.id ) As ans
From products_quotes As pq1
Left Join quotes As q1
On pq1.quote_id = q1.id
Where q1.deleted = 0
And pq1.deleted = 0
And q1.stage <> 4
And pq1.qty_shipped < pq1.product_qty
Group By pq1.product_id
) As customfields_oo
On customfields_oo.product_id = products.id
Left Join products_cstm
On products.id = products_cstm.id_c
Where products.deleted = 0
Order By customfields_oo.ans Desc
Now, you have stated in comments that this is too slow because, say products where deleted <> 0 might be evaluated in the derived table. If that is the case, then simply expand the derived table to include the filters on the outer products table.
Select products.id,
products_cstm.oem_c,
products.mfr_part_num,
products.description,
products.cost,
products.assigned_user_id,
customfields_oo.ans
FROM products
Left Join (
Select pq1.product_id
, Count( q1.id ) As ans
From products_quotes As pq1
Join products As p1
On p1.products.id = pq1.product_id
Left Join quotes As q1
On pq1.quote_id = q1.id
Where q1.deleted = 0
And pq1.deleted = 0
And q1.stage <> 4
And pq1.qty_shipped < pq1.product_qty
And p1.deleted = 0
Group By pq1.product_id
) As customfields_oo
On customfields_oo.product_id = products.id
Left Join products_cstm
On products.id = products_cstm.id_c
Where products.deleted = 0
Order By customfields_oo.ans Desc
You do not need to have AND pq.product_id = products.id in the where statement. Because you are LEFT JOINing on that. So I think something like this will work:
AND (pq.qty_shipped < pq.product_qty)
GROUP BY pq.product_id) AS customfields_oo
ON customfields_oo.product_id = products.id
LEFT JOIN products_cstm
ON products.id = products_cstm.id_c
WHERE products.deleted = 0
ORDER BY openorder DESC
EDIT
You do not need to LEFT JOIN on the table you are COUNTing on. You can also do ot like this:
SELECT
.....
(
SELECT
COUNT( q.id )
FROM products_quotes pq
LEFT JOIN quotes q
ON pq.quote_id = q.id
WHERE q.deleted = 0
AND pq.deleted = 0
AND q.stage <> 4
AND (pq.qty_shipped < pq.product_qty)
AND pq.product_id = products.id
) AS ans
FROM products
.....