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?
Related
Hi I am a beginner I am trying to make all the result row of my subquery that is null to return 0 not null. but I am getting an error. I will really appreciate any advice. Thank you
Error Code: 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 'AS 'Income'
SELECT
COUNT(DISTINCT t1.id) AS 'Val1',
COALESCE((SELECT SUM(CAST(COALESCE(r.t_payment_total,0) AS DECIMAL(18,2))) AS 'Income'
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status!="Pending" && r.t_status!="Booked" AND r.c_mid = m.id AND t.t_type_id = t1.t_type_id
),0)AS 'Income'
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id AND mp.t_type = 'try' AND mp.t_system_id = 'ok'
GROUP BY t1.t_tool_type_id
ORDER BY m.t_m ASC, tt.t_ttype ASC, mf.t_floor ASC;
Output I removed COALESCE I am having null
Val1 Income
10 Null
2 30
23 10
5 Null
Desired Output
Val1 Income
10 0
2 30
23 10
5 0
I would suggest moving that subquery into the from clause
SELECT
t1.t_tool_type_id
, COUNT( DISTINCT t1.id ) AS `Val1`
, COALESCE(i.Income , 0 ) AS `Income`
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id
AND mp.t_type = 'try'
AND mp.t_system_id = 'ok'
left join (
SELECT
CAST( SUM(r.t_payment_total) AS decimal(18, 2) ) AS `Income`
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status != 'Pending'
AND r.t_status != 'Booked'
AND r.c_mid = m.id
GROUP BY
t.t_type_id
) as i on t1.t_type_id = i.t_type_id
GROUP BY
t1.t_tool_type_id
;
nb: I have assumed r.t_payment_total is numeric.
I also suggest you always use single quotes for values/literals, and in MySQL backticks for identity (e.g. column headings) but really they are not essential in this query.
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
);
I have the following query I am trying to join 2 tables (' Industry' , 'Country' ) on 2 conditions, but it gives me the following error
Error Code: 1054. Unknown column 'i.id' in 'on clause'
Does anybody know how should I tackle this?
SELECT c.name AS country_name, i.name as industry_name, num_projects, num_consultants, admin_rating
FROM industry i, country c
JOIN (SELECT pc.country_id, pi.industry_id, COUNT(p.id) AS num_projects
FROM project p, project_country pc, project_industry pi
where p.id = pc.project_id and pi.project_id=p.id
GROUP BY pc.country_id,pi.industry_id) x ON x.country_id = c.id and x.industry_id=i.id
JOIN (SELECT u.country_id,ie.industry_id, COUNT(u.id) AS num_consultants
FROM user u, consultant_profile, industry_experience ie
WHERE u.is_active = 1 AND u.type = 0 and
ie.consultant_profile_id= consultant_profile.id
and u.id= consultant_profile.id
GROUP BY u.country_id,ie.industry_id) y ON y.country_id = c.id and y.industry_id = i.id order by num_projects DESC limit 20;
EDIT the table structure is as following:
industry - id
project_industry - industry_id, project_id
industry_experience - consultant_profile_id, industry_id
consultant_profile - id,user_id
Since you still did not provide any sql fiddle
you can start from my one:
http://sqlfiddle.com/#!9/6c0569/1
SELECT pc.country_id, pi.industry_id,
COUNT(p.id) AS num_projects,
COUNT(u.id) AS num_consultants
FROM project p
INNER JOIN project_country pc
ON p.id = pc.project_id
INNER JOIN project_industry pi
ON pi.project_id=p.id
INNER JOIN `user` u
ON u.is_active = 1 AND u.type = 0
and u.country_id = pc.country_id
INNER JOIN industry_experience ie
ON u.id = ie.consultant_profile_id
AND ie.industry_id = pi.industry_id
GROUP BY pc.country_id, pi.industry_id
if you will add some data into that fiddle we can discuss deeper
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'
I have a ticket purchasing database and want to collect all of the orders that meet certain criteria, and then also grab the total number of tickets each order has by counting the rows in the 'orders_tickets' table that match the orderID. If I make the SQL a simple call, this SQL works:
SQL A:
SELECT o . * , COUNT( ot.OrderTicketID ) AS numtix
FROM orders o
LEFT JOIN orders_tickets ot ON o.orderID = ot.orderID
GROUP BY o.orderID
LIMIT 0 , 30
And this SQL works too - this is the original SQL call with all the data I want except the numtix data:
SQL B:
SELECT o.*,
IF(o.orderedbyID = '0', ob.fname, u.fname) AS obfname,
IF(o.orderedbyID = '0', ob.lname, u.lname) AS oblname
FROM orders o, perfs p, orders_orderedby ob, users u
WHERE p.eventID = '2'
AND p.perfID = o.perfID
AND ( (UNIX_TIMESTAMP(p.perfdatetime) - UNIX_TIMESTAMP(NOW())) > 0)
AND ob.orderID = o.orderID
AND u.userID = o.orderedbyID
ORDER BY p.perfdatetime ASC
LIMIT 0, 30
But when I try to incorporate SQL A into SQL B, I get errors:
SQL C: (does not work)
SELECT o.*, COUNT( ot.OrderTicketID ) AS numtix,
IF(o.orderedbyID = '0', ob.fname, u.fname) AS obfname,
IF(o.orderedbyID = '0', ob.lname, u.lname) AS oblname
FROM orders o, perfs p, orders_orderedby ob, users u
LEFT JOIN orders_tickets ot ON o.orderID = ot.orderID
WHERE p.eventID = '2'
AND p.perfID = o.perfID
AND ( (UNIX_TIMESTAMP(p.perfdatetime) - UNIX_TIMESTAMP(NOW())) > 0)
AND ob.orderID = o.orderID
AND u.userID = o.orderedbyID
ORDER BY p.perfdatetime ASC
GROUP BY o.orderID
LIMIT 0, 30
This is the error I get:
#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 'GROUP BY o.orderID LIMIT 0, 30' at line 12
My instinct is that the "GROUP BY" needs to apply to the LEFT JOIN and not the entire SQL, but that's a sheer guess.
Any help is most appreciated!
ORDER BY goes after GROUP BY:
GROUP BY o.orderID
ORDER BY p.perfdatetime ASC
LIMIT 0, 30
Update:
SELECT o.*,
COUNT( ot.OrderTicketID ) AS numtix,
IF(o.orderedbyID = '0', ob.fname, u.fname) AS obfname,
IF(o.orderedbyID = '0', ob.lname, u.lname) AS oblname
FROM orders o
JOIN perfs p
ON p.perfID = o.perfID
JOIN orders_orderedby ob
ON ob.orderID = o.orderID
JOIN users u
ON u.userID = o.orderedbyID
LEFT JOIN
orders_tickets ot
ON ot.orderID = o.orderID
WHERE p.eventID = '2'
AND p.perfdatetime < NOW()
GROUP BY
o.orderID
ORDER BY
p.perfdatetime ASC
LIMIT 0, 30