MYSQL count child table with other modifiers - help please! - mysql

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

Related

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
);

Results returning twice from MySQL query

I'm struggling to figure out why my results are returning twice for the group messages. It's returning the correct values for the single conversations.
It should be returning all the data from Data in table screenshot. However it's returning the data with is_group = 1 twice.
Data in Table:
MySQL Query:
(SELECT dm.* FROM `direct_message`AS dm
INNER JOIN direct_message_thread AS dmt
ON dmt.chat_id = dm.id
WHERE
( dm.recipient_id = '10896' OR dm.creator_id = '10896' )
AND dm.school_id = '1'
GROUP BY dm.id
ORDER BY dmt.inserted DESC
) UNION ALL (
SELECT dm.* FROM `direct_message` AS dm
INNER JOIN direct_message_thread AS dmt ON dmt.chat_id = dm.id
LEFT JOIN direct_message_group AS dmg ON dmg.chat_id = dm.id
WHERE dmg.staff_id = '10896' AND dm.school_id = '1'
GROUP BY dm.id
ORDER BY dmt.inserted DESC
) LIMIT 0, 25
Results:
I think it could be because of the first SELECT getting the results and then the UNION ALL get the same results but not grouping together with the first SELECT
When I try and do the following:
(SELECT dm.* FROM `direct_message`AS dm
INNER JOIN direct_message_thread AS dmt
ON dmt.chat_id = dm.id
WHERE ( dm.recipient_id = '10896' OR dm.creator_id = '10896' )
AND dm.school_id = '1'
) UNION ALL (
SELECT dm.* FROM `direct_message` AS dm
INNER JOIN direct_message_thread AS dmt ON dmt.chat_id = dm.id
LEFT JOIN direct_message_group AS dmg ON dmg.chat_id = dm.id
WHERE dmg.staff_id = '10896' AND dm.school_id = '1'
)
GROUP BY dm.id
ORDER BY dmt.inserted DESC
LIMIT 0, 25
It shows this error message:
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 dm.id ORDER BY dmt.inserted DESC LIMIT 0, 25' at line
14
You are using union for two queries.
Records 595-597 and 599-601 are met both of them.
My be better to select to whole query with
select id,...
from (...)
group by id

MySQL: JOIN multiple tables

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

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;