GROUP_CONCAT not working with left join - mysql

This query is showing error :
#1054 - Unknown column 'sp.spot_id' in 'on clause'
Query :
SELECT product.*,sp.sp_name FROM `product`
left join spot_selling on product.product_id=spot_selling.product_id
AND spot_selling.end_time >= now()
AND spot_selling.start_time <= now()
AND spot_selling.status='1'
left join(select GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
from spot s group by s.spot_id) sp on sp.spot_id=spot_selling.spot_id
WHERE product.user_id='26' AND product.status!='6'

I think you just need spot_id in the subquery, so there is something to join on:
SELECT product.*, sp.sp_name
FROM `product` left join
spot_selling
on product.product_id=spot_selling.product_id AND
spot_selling.end_time >= now() AND
spot_selling.start_time <= now() AND
spot_selling.status = '1' left join
(select spot_id, GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
--------------^
from spot s
group by s.spot_id
) sp
on sp.spot_id = spot_selling.spot_id
WHERE product.user_id = '26' AND product.status <> '6'

Related

Cross Join then left Join (Uknown column)

What query would I do if this code is working on PHPMyAdmin SQL:
SELECT
DATE_FORMAT(d.date, '%b %e %Y') date,
u.employee_id, ai.time_in,
ao.time_out
FROM (SELECT date FROM hris_timein UNION SELECT date FROM hris_timeout order by date asc) d
CROSS JOIN hris_users u
LEFT JOIN hris_timein ai ON u.employee_id = ai.employee_id AND ai.date = d.date
LEFT JOIN hris_timeout ao ON u.employee_id = ao.employee_id AND ao.date = d.date
Output: see output
But when I use this code in my project, it displays an error:
Error description Unknown column 'd.date' in 'on clause'
The query looks correct, try it with quoted identifiers.
SELECT
DATE_FORMAT(d.`date`, '%b %e %Y') `date`,
u.`employee_id`,
ai.`time_in`,
ao.`time_out`
FROM
(
SELECT
`date`
FROM
`hris_timein`
UNION
SELECT
`date`
FROM
`hris_timeout`
) d
CROSS JOIN
`hris_users` u
LEFT JOIN
`hris_timein` ai
ON u.`employee_id` = ai.`employee_id` AND ai.`date` = d.`date`
LEFT JOIN
`hris_timeout` ao
ON u.`employee_id` = ao.`employee_id` AND ao.`date` = d.`date`;

mariadb access outer column in subquery

I have an issue with the following query that it is working on Mariadb 10.1.48 but it is not on 10.2.40.
It returns the following error
Error Code: 1054. Unknown column 'c.id' in 'where clause'
SELECT
p.id AS pid,
p.name AS pname,
c.id AS cid,
c.contract_code AS contr_code,
c.type AS ctype,
e.id AS eid,
e.state AS estate,
e.pay_code AS epay_code,
COUNT(z.id) AS damages,
rp.id AS resp_id,
rp.name AS resp_name,
rp.surname AS resp_surname,
IF(CURDATE() BETWEEN DATE_ADD(e.starts_pay_date, INTERVAL 23 DAY) AND DATE_ADD(e.starts_pay_date, INTERVAL 1 MONTH) AND e.state = 1, 1, 0) AS ending
FROM `contracts` AS c,
`customers` AS p,
`cash` AS e
LEFT JOIN `damage` z ON e.contract_id = z.contract_id AND z.state = 1
LEFT JOIN `customers` rp ON rp.id = (
SELECT MAX(p1.id) resp
FROM `customers` AS p1,
`contract_responsible` AS r
WHERE c.id = r.contract_id AND p1.id = r.resp_id )
WHERE e.starts_pay_date <= '2021-09-08 23:59:59' AND c.customer_id = p.id AND e.contract_id = c.id
GROUP BY e.id
ORDER BY e.inserted_date DESC
LIMIT 50 OFFSET 0
I know that it has to do with accessing outer column in a subquery, but I cannot understand why it used to work and now it is NOT!
Can you suggest a workaround?

SQL - GROUB BY - HAVING - MISSING ROWS

the following is the situation. I need to connect an order-table with a message-table. But i'm only interested in the first message(lowest message-id). The connection between the tables is the orderid.
$result = $this->db->executeS('
SELECT o.*, c.iso_code AS currency, s.name AS shippingMethod, m.message AS note
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'currency c ON c.id_currency = o.id_currency
LEFT JOIN '._DB_PREFIX_.'message m ON m.id_order = o.id_order
LEFT JOIN '._DB_PREFIX_.'carrier s ON s.id_carrier = o.id_carrier
LEFT JOIN jtl_connector_link l ON o.id_order = l.endpointId AND l.type = 4
WHERE l.hostId IS NULL AND o.date_add BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND NOW()
GROUP BY o.id_order
HAVING MIN(m.id_message)
LIMIT '.$limit
);
This query works so far. But now orders without a message are missing.
Thank you for your help!
Markus
You want to select several orders and per order the first message. This is generally difficult in MySQL for the lack of window functions (e.g. ROW_NUMBER OVER). But as it's just one column from the message table you are interested in, you can use a subquery in the SELECT clause.
SELECT
o.*,
c.iso_code AS currency,
s.name AS shippingMethod,
(
SELECT m.message
FROM message m
WHERE m.id_order = o.id_order
ORDER BY m.id_message
LIMIT 1
) AS note
FROM orders o
JOIN currency c ON c.id_currency = o.id_currency
JOIN carrier s ON s.id_carrier = o.id_carrier
WHERE o.date_add BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND NOW()
AND NOT EXISTS
(
SELECT *
FROM jtl_connector_link l
WHERE l.endpointId = o.id_order
AND l.type = 4
);

Error Code: 1242 Subquery returns more than 1 row

Got problem, I am trying to calculate of each employee Total_Hours but when am grouping it comes up with an error. However, I know what the problem is but i am not sure how to fix it.
Select
tt.uid as 'Employee',
if (tc.organisation like '%Village%' or tc.organisation like '%Personnel%', 'Village', 'Client' ) as 'Type of work',
round((sum(timestampdiff(minute, start_time, end_time))/60),2) AS 'Hours',
(select
round((sum(timestampdiff(minute, start_time, end_time))/60),2)
from timesheet.timesheet_times ttt
left outer join timesheet.timesheet_project ttp on ttt.proj_id = ttp.proj_id
left outer join timesheet.timesheet_client ttc on ttp.client_id = ttc.client_id
where
month(ttt.start_time) = month(now())
and year(ttt.start_time) = year(now())
group by
ttt.uid
)as TOTAL_HOURS_PER_EMPLOYEE,
round((
(round((sum(timestampdiff(minute, start_time, end_time))/60),2)
/
(select
round((sum(timestampdiff(minute, start_time, end_time))/60),2)
from timesheet.timesheet_times ttt
left outer join timesheet.timesheet_project ttp on ttt.proj_id = ttp.proj_id
left outer join timesheet.timesheet_client ttc on ttp.client_id = ttc.client_id
where
month(ttt.start_time) = month(now())
and year(ttt.start_time) = year(now()))
)*100),0) as percentage
from
timesheet.timesheet_times tt
left outer join timesheet.timesheet_project tp on tt.proj_id = tp.proj_id
left outer join timesheet.timesheet_client tc on tp.client_id = tc.client_id
where
month(tt.start_time) = month(now())
and tc.organisation not like '%Private%'
and year(tt.start_time) = year(now())
group by
tc.client_id
order by
round((sum(timestampdiff(minute, start_time, end_time))/60),2) desc
I am sorry if I wrote that post wrong, it's my first time on here or asked for help.

MySQL throwing error on second JOIN

I'm getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN product_catalog ON product_catalog.entity_id
As a result of the following query:
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id, SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
WHERE sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
WHERE product_catalog.size = 14
GROUP BY order_item.order_id;
Variations on this query have worked for grouping different types of product by sales order in the past where I only needed to perform one JOIN to get all the info I needed. The problem I'm encountering is from the second JOIN. Clearly I'm missing something but I really am not sure what. :(
Please make sure that WHERE condition must be after all JOIN
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id, SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
WHERE product_catalog.size = 14
AND sales_order.created_at > '2012-11-15 00:00:00'
GROUP BY order_item.order_id;
First of all you have to JOIN your tables which you need. Then after WHERE clause come for conditions.
Your WHERE clauses are in the wrong spots. See the code below for proper JOIN syntax.
SELECT sales_order.created_at,
order_item.order_id,
sales_order.increment_id,
SUM(order_item.qty_ordered) AS qty_ordered,
COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
AND sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
AND product_catalog.size = 14
GROUP BY order_item.order_id
JOIN...ON... clause it's also section to input condition so you don't need where clause, just add AND instead.
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id,
SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order ON sales_order.entity_id = order_item.order_id
and sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog ON product_catalog.entity_id = order_item.product_id
and product_catalog.size = 14
GROUP BY order_item.order_id;
Please consider below example I added aliases. It's good practice to use it because code is more readable.
SELECT SO.created_at , OI.order_id, SO.increment_id,
SUM(OI.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item OI
JOIN sales_order SO ON SO.entity_id = OI.order_id
and SO.created_at > '2012-11-15 00:00:00'
JOIN product_catalog PC ON PC.entity_id = OI.product_id
and PS.size = 14
GROUP BY OI.order_id;