I have a table called TableName which I want to delete from all rows that SUM of their product quantity is less than 2.
I need to inner join table oc_order_product and SUM the values having the same product_id then use this SUM value in where clause to delete all rows with SUM less than 2.
I am using the following query right now:
Delete TablenName from TablenName
INNER JOIN oc_order_product
ON oc_order_product.product_id = TablenName.product_id
where oc_order_product.quantity HAVING SUM(oc_order_product.quantity) < 2;
Which I am getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax
to use near 'HAVING SUM(oc_order_product.quantity) < 2' at line 4
Hmmm . . . If you need to do aggregation, you need to do it before the join:
Delete t
from TablenName t join
(select op.product_id, sum(op.quantity) as sumquantity
from oc_order_product op
group by op.product_id
) op
on op.product_id = t.product_id and op.sumquantity < 2;
Related
I want to update two tables at the same time, because the connection and update conditions are in the first table. And only simple information like the amount is in the second table
UPDATE
order,
order_product
SET
order.total = order.total*0.00001,
order_product.price = order_product.price*0.00001,
order_product.total = order_product.total*0.00001
FROM
order_product
LEFT JOIN
order ON order_product.order_id = order.order_id
WHERE
order.currency_code = "USD"
AND
order.currency_value = 0.00001000
I keep getting this error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM
order_product
LEFT JOIN
order ON order_id ...' at line 5
Your syntax is off for MySQL and looks to be taken from some other dialect. For a MySQL update join, use an explicit join as follows:
UPDATE order o
INNER JOIN order_product op
ON op.order_id = o.order_id
SET
o.total = o.total*0.00001,
op.price = op.price*0.00001,
op.total = op.total*0.00001
WHERE
o.currency_code = 'USD' AND
o.currency_value = 0.00001000;
I'm trying to update my riders total ucipoints by following an example, with some small modifications, but I just get an error from it.
example
UPDATE P
SET extrasPrice = t.TotalPrice
FROM BookingPitches AS P INNER JOIN
(
SELECT
PitchID,
SUM(Price) TotalPrice
FROM
BookingPitchExtras
GROUP BY PitchID
) t
ON t.PitchID = p.ID
I got the code from this answer:
SQL Update to the SUM of its joined values
My code looks like this:
UPDATE P
SET ucipoeng = t.TotalPoints
FROM rytterlagsesong AS P INNER JOIN
(
SELECT
rytterid,
SUM(poeng) AS TotalPoints
FROM
t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t
ON t.rytterid = P.rytterid AND t.sesong='2016'
I get the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM rytterlagsesong AS P INNER JOIN ( SELECT rytterid, SUM(ucip' at line 3
Can someone help me find the error?
DB Structure:
rytterlagsesong: rytterid - ucipoeng - sesong
t_ucipoeng: rytterid - dato - poeng
So I want to sum the points (poeng) of all races in 2016 (dato=date) for a rider
And update that riders totalpoint (ucipoeng) for this season (sesong)
The update / join syntax is different per database. For mysql, use this:
UPDATE rytterlagsesong r
INNER JOIN (
SELECT rytterid, SUM(poeng) AS TotalPoints
FROM t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t ON t.rytterid = r.rytterid AND t.sesong='2016'
SET r.ucipoeng = t.TotalPoints
I am trying to select data from one table with the results of the data that is coming out from joining two different tables, but I am getting an error and I can not figure it out:
SELECT CONCAT(firstname,lastname) AS staffname
FROM `cms_users`
RIGHT JOIN
(SELECT DISTINCT CONCAT(a.firstname," ",a.lastname) AS clientname, b.doa
FROM `cms_clients` a
INNER JOIN `cms_question_report` b
ON a.id = b.cid
WHERE b.doa < '2015-04-15 23:00:00' AND b.doa > '2015-04-09 00:00:00') incidents
The error I am getting is:
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 'LIMIT 0, 30' at line 9
But I don't even have a LIMIT 0,30 in my query.
Your query is incomplete. You are missing the joining columns for your right join. See below, but you must replace PRIMARY_KEY with the primary key for your cms_users table. Also make sure that i.id is the correct joining column for the two tables.
SELECT CONCAT(firstname,lastname) AS staffname
FROM `cms_users`
RIGHT JOIN
(SELECT DISTINCT CONCAT(a.firstname," ",a.lastname) AS clientname, b.doa
FROM `cms_clients` a
INNER JOIN `cms_question_report` b
ON a.id = b.cid
WHERE b.doa < '2015-04-15 23:00:00' AND b.doa > '2015-04-09 00:00:00')
AS i ON i.id = cms_users.PRIMARY_KEY
I hope that helps!
I have this query and appearently it's faulty?
I'm trying to join fices with mems so I can have the ficeID along with all the results from mems(These queries work individually). What am I doing wrong?
SELECT *
FROM mems
WHERE deleted <> -1
ORDER BY sort_mems
LEFT JOIN SELECT ficeID
FROM fices
Result:
#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 'LEFT JOIN SELECT ficeID FROM offices LIMIT 0, 30' at line 1
You have JOIN clause after ORDER BY. You should place it in FROM.
I suggest you define condition of a LEFT JOIN
Also, I suggest you surround you temp tables with brackets:
SELECT m.*, t1.officeID
FROM members m
LEFT JOIN offices t1
ON m.memberID = t1.memberID
WHERE m.deleted <> -1
ORDER BY m.sort_membername;
Yes, you have the LEFT JOIN in the wrong spot (it should go after your FROM clause, and you also seem to be missing a join criteria (the ON part, this tells the database how these tables are related):
SELECT *
FROM mems m
LEFT JOIN fices f
ON m.??? = f.???
WHERE deleted <> -1
ORDER BY sort_mems
I have the following query:
SELECT * FROM
(SELECT t1.`id`, t1.`vehicle`, lp1.`veh_no` AS `lp_vehicle`,
t1.`last_date`, t1.`due_date`, t1.`driver`, lp4.`employ_name` AS `lp_driver`
FROM `inspection` AS t1 LEFT OUTER JOIN `vehicle` AS lp1
ON (t1.`vehicle` = lp1.`id`)
LEFT OUTER JOIN `employee_driver` AS lp4
ON (t1.`driver` = lp4.`id`)) subq,
WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER by vehicle asc;
It processes through fine until I get to the WHERE clause.
This is what I get on the above:
#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
'WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER b'
at line 1
Can someone please point out what I am doing wrong? I'm running MySQL 5.1.48
you have an extra comma after subq