UPDATE item t
INNER JOIN ( SELECT
item_name,
MAX( item_keyword ) AS item_keyword
FROM item
WHERE ca_id2 = '2010'
GROUP BY item_name
) s ON t.item_name = s.item_name
SET t.item_keyword = s.item_keyword
WHERE t.ca_id2 ='3010'
The error is:
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 'SELECT item_name, max(item_keyword) AS item_keyword
FROM item
MySQL version 4.0.22 What is the reason for the failure?
I tried to construct your query based on
http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/
UPDATE item t
SET t.item_keyword =
( SELECT MAX( i.item_keyword ) AS item_keyword
FROM ( SELECT item_keyword, item_name, cd_id2 FROM item ) AS i
WHERE t.item_name = i.item_name
AND i.cd_id2 = '2010'
GROUP BY i.item_name
)
WHERE t.ca_id2 ='3010'
Or you can try this version too
UPDATE item t
INNER JOIN ( SELECT item_keyword, item_name, cd_id2 FROM item ) AS i
SET t.item_keyword =
( SELECT MAX( i.item_keyword ) AS item_keyword
FROM i
WHERE t.item_name = i.item_name
AND i.cd_id2 = '2010'
GROUP BY i.item_name
)
WHERE t.ca_id2 ='3010'
Sorry, I don't have MySQL 4 around, so you have to test the above queries.
Other readings:
http://dev.mysql.com/doc/refman/4.1/en/update.html
http://dev.mysql.com/doc/refman/4.1/en/rewriting-subqueries.html
If the above queries don't work, you can try using a temporary table instead of the subselect:
http://sqlfiddle.com/#!2/13ccb/1
CREATE TEMPORARY TABLE s
SELECT item_name,
MAX( item_keyword ) AS item_keyword
FROM item
WHERE cd_id2 = '2010'
AND item.item_name IN ( SELECT item_name FROM item WHERE item.ca_id2='3010')
GROUP BY item_name;
UPDATE item t
INNER JOIN s ON t.item_name = s.item_name
SET t.item_keyword = s.item_keyword
WHERE t.ca_id2 ='3010';
DROP TEMPORARY TABLE s;
Related
I am running a query against a table and doing a left join to try and get the record from the left table with the most recent date but it's not picking up the other values relevant to the datetime column (user and notes)
SELECT
i.customer_sequence,
i.due_date,
MAX(cn.datetime) as notes_datetime,
cn.user as notes_user,
cn.notes as notes_notes
FROM
billing_invoices i
LEFT JOIN customer_notes cn
ON i.customer_sequence = cn.customer_seq
WHERE
cn.type = 'Accounts' AND
i.customer_sequence <> '0' AND
i.status = 'Unpaid' AND
i.directdebit <> 'Y'
GROUP BY
i.customer_sequence
ORDER BY
i.due_date DESC
Aggregation is not the solution here. You want the entire row from the joined table, so this suggest filtering instead. If you are running MySQL 8.0, I would recommend window functions:
SELECT *
FROM (
SELECT
i.customer_sequence,
i.due_date,
ROW_NUMBER() OVER(PARTITION BY i.customer_sequence ORDER BY cn.datetime DESC) rn,
cn.datetime as notes_datetime,
cn.user as notes_user,
cn.notes as notes_notes
FROM billing_invoices i
LEFT JOIN customer_notes cn
ON i.customer_sequence = cn.customer_seq
AND cn.type = 'Accounts'
WHERE
i.customer_sequence <> '0' AND
i.status = 'Unpaid' AND
i.directdebit <> 'Y'
) t
ORDER BY i.due_date DESC
Note that I moved the condition on the left joined table from the WHERE clause to the ON clause of the join (otherwise, this acts like an inner join).
In earlier versions, one option is a correlated subquery:
SELECT
i.customer_sequence,
i.due_date,
cn.datetime as notes_datetime,
cn.user as notes_user,
cn.notes as notes_notes
FROM billing_invoices i
LEFT JOIN customer_notes cn
ON i.customer_sequence = cn.customer_seq
AND cn.type = 'Accounts'
AND cn.datetime = (
SELECT MAX(cn1.datetime)
FROM customer_notes cn1
WHERE i.customer_sequence = cn1.customer_seq AND cn1.type = 'Accounts'
)
WHERE
i.customer_sequence <> '0' AND
i.status = 'Unpaid' AND
i.directdebit <> 'Y'
I'm struggling to get my nested SQL statement to work.
It tells me I have a syntax error near the second SELECT.
Can anyone advise?
SELECT date, Expense_Date, cost1
RIGHT JOIN t ON dates.date = t.Expense_Date
FROM (
SELECT Expense_Date, IFNULL(Sum(Total_Cost),0) as cost1
FROM Expenses
RIGHT JOIN membership_userrecords ON Expenses.id = membership_userrecords.pkValue
where membership_userrecords.memberID = 'kieran' AND membership_userrecords.tableName='Expenses'
GROUP BY Expense_Date
) AS t
GROUP BY date ;
Forget about second SELECT ... You are missing a FROM clause in your first SELECT itself as seen below
SELECT date, Expense_Date, cost1 <-- here
RIGHT JOIN t ON dates.date = t.Expense_Date
Try this
SELECT date, Expense_Date, cost1
FROM (
SELECT Expense_Date, IFNULL(Sum(Total_Cost),0) as cost1
FROM Expenses
RIGHT JOIN membership_userrecords ON Expenses.id = membership_userrecords.pkValue
where membership_userrecords.memberID = 'kieran' AND membership_userrecords.tableName='Expenses'
GROUP BY Expense_Date
) AS t
LEFT JOIN t ON dates.date = t.Expense_Date
GROUP BY date ;
I want to use the default search and pagination in yii2. But the query is complex and I don't know how can I add it to the search model! This is the query:
SELECT p.*,po_sum,rpo_sum,so_sum
FROMproduct p
LEFT JOIN (
SELECT id,product_id , IF(sum(quantity) IS NULL, 0, sum(quantity)) AS po_sum
FROM purchase_order_products inner join purchase_order on purchase_order.id = purchase_order_products.purchase_order_id
Where purchase_order.status = 'Approved'
GROUP BY product_id )
subcount ON p.id = subcount.product_id
LEFT JOIN (
SELECT id,product_id , sum(quantity) AS rpo_sum
FROM return_purchase_order_products inner join return_purchase_order on return_purchase_order.id = return_purchase_order_products.purchase_order_id Where return_purchase_order.status = 'Approved'
GROUP BY product_id )
subcount2 ON p.id = subcount2.product_id
LEFT JOIN (
SELECT product_id , sum(quantity_ordered) AS so_sum
FROM sales_order_item inner join sales_order on sales_order.id = sales_order_item.sales_order_id Where sales_order.order_status = 'complete'
GROUP BY product_id )
subcount3 ON p.id = subcount3.product_id
order by po_sum DESC,rpo_sum DESC
Any help?
If you use MySql >= 5.7.7 the easiest way is to create a view with that query and use it in the tableName method.
You need that version of MySql because you cant use subquery in from clause during view creation in previous versions.
I have a SQL Query. When I run it executes successfully, but when i was trying to create a view with the query it shows create a view with each subquery.
SOS.
SELECT `product_category_vendor_id`,
ct1.category_id,
ct1.category_name,
c1.`product_id`,
p1.product_name,
c1.`vendor_id`,
v1.vendor_name,
c1.`product_purchase_price`,
c1.product_vendor_qty,
d1.available
FROM (SELECT `product_category_vendor_id`,
a1.`product_id`,
a1.`vendor_id`,
a1.`product_purchase_price`,
b2.product_vendor_qty
FROM (SELECT `product_category_vendor_id`,
`product_id`,
`vendor_id`,
`product_purchase_price`
FROM tbl_inksand_product_category_vendor
WHERE product_category_vendor_id IN
(SELECT
Max(product_category_vendor_id) AS
`Product_Category_Vendor_Id`
FROM
tbl_inksand_product_category_vendor
WHERE (
product_category_vendor_status =
'A' )
GROUP BY product_id,
`vendor_id`
ORDER BY `product_id`))
AS a1
JOIN (SELECT `product_id`,
`vendor_id`,
Sum(`product_vendor_qty`) AS Product_Vendor_Qty
FROM tbl_inksand_product_category_vendor
GROUP BY product_id,
`vendor_id`
ORDER BY `product_id`) b2
ON a1.`product_id` = b2.`product_id`
AND a1.`vendor_id` = b2.`vendor_id`) c1
JOIN (SELECT `product_id`,
`vendor_id`,
Count(`item_id`) AS Available
FROM `tbl_inksand_item_dtls`
WHERE `product_item_status` = 'Not Sale'
GROUP BY `product_id`,
`vendor_id`) d1
ON c1.`product_id` = d1.`product_id`
AND c1.`vendor_id` = d1.`vendor_id`
JOIN tbl_inksand_vendor AS v1
ON c1.`vendor_id` = v1.`vendor_id`
JOIN tbl_inksand_product p1
ON p1.product_id = c1.`product_id`
JOIN tbl_inksand_category ct1
ON ct1.category_id = p1.category_id
Subqueries cannot be used in the FROM clause of a view.
Source: https://dev.mysql.com/doc/refman/5.0/en/view-restrictions.html
So yeah it was the right reccomendation to create seperate views.
I'm having a bit of a problem with the following MySQL query and I can't find the source of it.
MySQL tells me that
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'annonce_dispo_id'
SELECT MAX(max_price) AS `max_price`,
COUNT(*) AS `nb_annonces`,
SUM(nb_dispo) AS `nb_dispo`
FROM
(SELECT `annonce`.`id`,
CEIL(MAX(price)*1.16) AS `max_price`,
COUNT(DISTINCT annonce.id) AS `nb_annonces`,
COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
`annonce_dispo1`.*,
`annonce_dispo2`.*
FROM `annonce`
LEFT JOIN `annonce_dispo` AS `annonce_dispoo` ON (annonce_dispoo.annonceId = annonce.id
AND STR_TO_DATE(annonce_dispoo.dispo_date, '%d/%m/%Y') >= CURDATE())
INNER JOIN `annonce_dispo` AS `annonce_dispo1` ON annonce.id = annonce_dispo1.annonceId
INNER JOIN `annonce_dispo` AS `annonce_dispo2` ON annonce.id = annonce_dispo2.annonceId
WHERE ((annonce.city IN
(SELECT `cities`.`id`
FROM `cities`
WHERE (cities.label LIKE 'lyon%'))
OR annonce.zipcode = 'lyon')
OR (annonce.city LIKE '28674'
OR annonce.zipcode = '28674'))
AND (annonce_dispo1.dispo_date = '27/05/2014')
AND (annonce_dispo1.disponibility = 'available')
AND (annonce_dispo2.dispo_date = '31/05/2014')
AND (annonce_dispo2.disponibility = 'available')
AND (annonce.visible = 1)
AND (annonce.completed = 1)
GROUP BY `annonce`.`id` HAVING (nb_dispo >= 1)) AS `t`
I thought gave a different alias for the table in each JOIN I use them in, and can't really put my finger on what else is possible to output such an error.
Don't select annonce_dispo1.* and annonce_dispo2.* in your subquery, duplicated column names are being returned. Instead select the fields you need and alias accordingly.
SELECT MAX(max_price) AS `max_price`,
COUNT(*) AS `nb_annonces`,
SUM(nb_dispo) AS `nb_dispo`
FROM
(SELECT `annonce`.`id`,
CEIL(MAX(price)*1.16) AS `max_price`,
COUNT(DISTINCT annonce.id) AS `nb_annonces`,
COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
`annonce_dispo1`.field, `annonce_dispo1`.otherfield,
`annonce_dispo1`.field as field2, `annonce_dispo1`.otherfield as otherfield2
FROM `annonce`
LEFT JOIN `annonce_dispo` AS `annonce_dispoo` ON (annonce_dispoo.annonceId = annonce.id
AND STR_TO_DATE(annonce_dispoo.dispo_date, '%d/%m/%Y') >= CURDATE())
INNER JOIN `annonce_dispo` AS `annonce_dispo1` ON annonce.id = annonce_dispo1.annonceId
INNER JOIN `annonce_dispo` AS `annonce_dispo2` ON annonce.id = annonce_dispo2.annonceId
WHERE ((annonce.city IN
(SELECT `cities`.`id`
FROM `cities`
WHERE (cities.label LIKE 'lyon%'))
OR annonce.zipcode = 'lyon')
OR (annonce.city LIKE '28674'
OR annonce.zipcode = '28674'))
AND (annonce_dispo1.dispo_date = '27/05/2014')
AND (annonce_dispo1.disponibility = 'available')
AND (annonce_dispo2.dispo_date = '31/05/2014')
AND (annonce_dispo2.disponibility = 'available')
AND (annonce.visible = 1)
AND (annonce.completed = 1)
GROUP BY `annonce`.`id` HAVING (nb_dispo >= 1)) AS `t`
See here for an example that doesn't work:
http://sqlfiddle.com/#!2/9bb13/1
The problem is that you are selecting all columns in the tables annonce_dispo1 and annonce_dispo2.
The fact that you have attributed different table names doesn't mean that there aren't duplicate column names.
I mean, you should use [Table name].[column name]
Example:
(SELECT `annonce`.`id`,
CEIL(MAX(price)*1.16) AS `max_price`,
COUNT(DISTINCT annonce.id) AS `nb_annonces`,
COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
`annonce_dispo1`.annonce_dispo_id AS `column1`,
`annonce_dispo2`.annonce_dispo_id AS `column2`
I hope I've helped