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
Related
I have this query and I am getting error #1066 - Not unique table/alias: 'components'. What seems to be the issue?
SELECT COUNT(*) FROM `products`, `components`, `tradeNames`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id
INNER JOIN `components` ON componentsMap.component_id = components.id
INNER JOIN `tradeNamesMap` ON .tradeNamesMap.product_id = products.id
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id
WHERE (((((LOWER(inci) LIKE '%abies%')
OR (trade_name.LOWER(name) LIKE '%abies%'))
OR (components.LOWER(no_cas)='abies'))
OR (components.LOWER(no_einecs)='abies'))
OR (components.LOWER(name)='abies'))
AND (`published`=1)
ORDER BY `trade_name`.`name` DESC
You don't need to list the tables before the INNER JOINs. In fact, simply don't ever use commas in the FROM clause. So:
SELECT COUNT(*)
FROM `products`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id
INNER JOIN `components` ON componentsMap.component_id = components.id
INNER JOIN `tradeNamesMap` ON tradeNamesMap.product_id = products.id
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id
WHERE (((((LOWER(inci) LIKE '%abies%')
OR (trade_name.LOWER(name) LIKE '%abies%'))
OR (components.LOWER(no_cas)='abies'))
OR (components.LOWER(no_einecs)='abies'))
OR (components.LOWER(name)='abies'))
AND (`published`=1)
ORDER BY `trade_name`.`name` DESC;
The above query only returns one row because of the COUNT(). The order by suggests that you actually want this information for each trade_name.name. If so, you need a GROUP BY:
SELECT tn.name, COUNT(*)
FROM `products` p INNER JOIN
`componentsMap cm
ON cm.product_id = p.id INNER JOIN
`components` c
ON cm.component_id = c.id INNER JOIN
`tradeNamesMap` tnm
ON tnm.product_id = p.id INNER JOIN
`tradeNames` tn
ON tnm.tradeName_id = tn.id
WHERE ((LOWER(inci) LIKE '%abies%') OR
(tn.LOWER(name) LIKE '%abies%') OR
(c.LOWER(no_cas)='abies') OR
(c.LOWER(no_einecs)='abies') OR
(c.LOWER(name)='abies')
) AND
(`published` = 1)
GROUP BY tn.name
ORDER BY tn.`name` DESC
INNER JOIN `[components]` ON componentsMap.component_id = components.id
AND
SELECT COUNT(*) FROM `products`, [`components`], `tradeNames`
Two components are there.
Just guessing, and untested, but I suspect that something like this would do what you're after...
SELECT n.name
, COUNT(*)
FROM products p
JOIN componentsMap pc
ON pc.product_id = p.id
JOIN components c
ON c.id = pc.component_id
JOIN tradeNamesMap pn
ON pn.product_id = p.id
JOIN tradeNames n
ON n.id = pn.tradeName_id
WHERE
( inci LIKE '%abies%'
OR n.name LIKE '%abies%'
OR 'abies' IN (c.no_cas,c.no_einecs,c.name)
)
AND published = 1
GROUP
BY n.name
ORDER
BY n.name DESC
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 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;
---------^
I have a query that shows a list of revisions and employees for every revision..
Now I'm trying to show if the given employee already has a row in the answers table..
This is the overview of the database
This is my working query that shows the list of revisions and employees
SELECT l.id, l.naam, r.id as revision_id, r.beschrijving, e.id as employee_id, e.voornaam, e.achternaam,
FROM lists l
INNER JOIN revisions r ON l.id = r.list_id
INNER JOIN employeelists el ON el.list_id= l.id
INNER JOIN employees e ON e.id = el.employee_id
INNER JOIN customers c ON c.id = e.customer_id
WHERE customer_id = :id AND r.actief = 1
Now I've tried several things to see if the employee already has a record in the answers table.. But It's failing the whole time..
Try 1 : Adding the Answers table with a left outer join
SELECT l.id, l.naam, r.id as revision_id, r.beschrijving, e.id as employee_id, e.voornaam, e.achternaam,
**CASE WHEN a.coach_id != 0 THEN 1 ELSE 0 END as FILLED IN**
FROM lists l"""
INNER JOIN revisions r ON l.id = r.list_id
**LEFT OUTER JOIN answers a ON a.revision_id = r.id**
INNER JOIN employeelists el ON el.list_id= l.id
INNER JOIN employees e ON e.id = el.employee_id
INNER JOIN customers c ON c.id = e.customer_id
WHERE customer_id = :id AND r.actief = 1
now the problem is that every employee is shown multiple times...
This is the SQLFiddle of the working database, The only thing i can't do is check if the given employee ( werknemer ) exists in the answers ( antwoorden ) table..
http://sqlfiddle.com/#!2/0c01c/4
Any idea on how i can solve this? I tried a subquery, but that didn't work out either.. Thanks!
Problem with Query Now
I thought i was correct but there's one more error. in the answers table, it shows results for werknemer_id ( employee_id ) = 78. For the revisie ( revision ) 1 and 2
While there is only results for revisie 1 (screenshot below)
Thanks!
How about this exist column will have 0 if not in antwoorden and 1 if exist
SELECT l.id, l.naam, r.revisie as revisie, r.id as revisie_id, r.beschrijving, w.id as werknemer, w.voornaam, w.achternaam
, a.werknemer_id,
(CASE WHEN a.werknemer_id IS NULL THEN 0 ELSE 1 END ) AS `exist`
FROM lijsten l
INNER JOIN revisies r ON l.id = r.lijst_id
INNER JOIN werknemerlijsten wl ON wl.lijst_id = l.id
INNER JOIN werknemers w ON w.id = wl.werknemer_id
INNER JOIN klanten k ON k.id = w.klant_id
LEFT JOIN antwoorden a ON w.id = a.werknemer_id
WHERE klant_id = 39 AND r.actief = 1
GROUP BY r.beschrijving, w.id
Fiddle
Final solution
This is the final outcome, only see 'ingevuld' is 1 if the desired coach (1 in thise case) is in the answers table.
SELECT l.id, l.naam, r.revisie AS revisie, r.id AS revisie_id, r.beschrijving, w.id AS werknemer, w.voornaam, w.achternaam, a.coach_id,
CASE WHEN a.coach_id = 1 THEN 1 ELSE 0 END AS ingevuld
FROM lijsten l
INNER JOIN revisies r ON l.id = r.lijst_id
INNER JOIN werknemerlijsten wl ON wl.lijst_id = l.id
INNER JOIN werknemers w ON w.id = wl.werknemer_id
INNER JOIN klanten k ON k.id = w.klant_id
LEFT JOIN antwoorden a ON w.id = a.werknemer_id AND r.id=a.revisie_id
WHERE klant_id = 39 AND r.actief = 1
group by r.id, w.id, a.coach_id
$q = "SELECT s.id, s.title, s.description,
(SELECT COUNT(*) FROM ".FORUM_THREADS." t WHERE t.cat_id = s.id) AS topics,
(SELECT COUNT(*) FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
WHERE t.cat_id = s.id) AS replies,
(SELECT r.date FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
WHERE t.cat_id = s.id ORDER BY r.date DESC LIMIT 1) AS last_post
FROM ".FORUM_SUBCATEGORIES." s WHERE s.parent = '$catid' AND s.status = '0' ORDER BY s.id";
I am attempting to select more than one field on the following part of the query
(SELECT r.date FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
INNER JOIN ".TBL_USERS." u ON u.id = r.author WHERE t.cat_id = s.id ORDER BY r.date DESC LIMIT 1) AS last_post
Along with r.date, I want to select u.username and r.author.
How can I go about doing this?
Thanks!
Just add them to the SELECT:
(SELECT r.date, r.author, u.username FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
INNER JOIN ".TBL_USERS." u ON u.id = r.author WHERE t.cat_id = s.id ORDER BY r.date DESC LIMIT 1) AS last_post
UPDATED after comment from OP:
You need to do 3 separate selects OR (depending on your data model) change the query so that the last_post query ends up after/in the FROM clause (there it can have as many columns as you want)...
Luke, you have a central select statement which uses nested select statements for getting the count. You can't depend on the nested select statements to count as the inner join, so you're going to have to add them to the central select statement instead.
In other words, join ".FORUM_REPLIES." and "u" (not sure what that's supposed to represent) with ".FORUM_SUBCATEGORIES.". I'd write the query for you, but I don't know how to link subcategories with replies and subcategories with u.