I am trying to update a table with a minimum date vale from another table.
I have tried:
UPDATE `profiles` AS t1
INNER JOIN (SELECT MIN(attendance_data.Logdate) min_date
FROM attendance_data) t2 ON t1.user_id=T2.user_id
SET t1.first_scan_date=t2.min_date
And get Unknown column 't2.user_id' in 'on clause'
and also
UPDATE `profiles` t1
SET first_scan_date = (SELECT MIN(t2.Logdate)
FROM attendance_data t2
WHERE t1.user_id = t2.user_id
Any advice?
Try this:
UPDATE `profiles` AS t1
INNER JOIN (SELECT user_id, MIN(attendance_data.Logdate) min_date
FROM attendance_data
group by user_id) t2 ON t1.user_id=T2.user_id
SET t1.first_scan_date=t2.min_date
Note that the right-hand side expression of inner join will return the minimum value of logdate for the particular user_id, and not the overall minimum.
If you just want to set all values in profiles to the minimum date, try this:
UPDATE `profiles` AS t1
SET t1.first_scan_date= (SELECT MIN(attendance_data.Logdate) min_date FROM attendance_data)
Related
I have a table named titles with 3 columns named titleID, price, and pubDate
I've been trying to change the price of a row to the same price as the row with the most recent pubDate
I have
UPDATE titles t
INNER JOIN titles t2
ON t.titleID = t2.titleID
SET
t.price = (
/*Returns the price of the titleID with the most recent date in pubID*/
SELECT t3.price FROM titles t3
ORDER BY t.pubDate DESC LIMIT 1)
WHERE t.titleID = 1001
The error I am getting is
Error Code: 1053. You can't specify target table t for update in FROM clause
How can I correctly change a value to a value from the same column
One workaround to this error is to use an update join:
UPDATE titles t1
INNER JOIN
(
SELECT titleID, MAX(pubDate) AS maxPubDate
FROM titles
GROUP BY titleID
) t2
ON t2.titleID = t1.titleID AND
INNER JOIN titles t3
ON t3.titleID = t1.titleID AND
t3.pubDate = t2.maxPubDate
SET
price = t3.price
WHERE
price <> t3.price;
Another solution is to add an outer query in the select statement:
UPDATE titles t
INNER JOIN titles t2
ON t.titleID = t2.titleID
SET
t.price = ( SELECT t1.price FROM (
SELECT t3.price
FROM titles t3
ORDER BY t.pubDate DESC LIMIT 1) as t1 )
WHERE t.titleID = 1001
For more details check: https://dev.mysql.com/doc/refman/8.0/en/update.html
Both of the above answers didn't give me what I was looking for but they really helped in pointing me in the right direction
Here's the code that finally changed the price, I think I was searching max date row incorrectly I think the first time on top of the other errors as well
UPDATE titles t1
INNER JOIN titles t2
ON t1.titleID = t2.titleID
SET t1.price = ( SELECT t4.price FROM (
SELECT t5.price
FROM titles t5
WHERE t5.pubDate = ( SELECT MAX(t3.pubDate)
FROM titles t3))
AS t4)
WHERE t1.titleID = 1001
Here's the code that fetches the price from the row with the max date
SELECT t5.price
FROM titles t1
WHERE t5.pubDate = ( SELECT MAX(t3.pubDate)
FROM titles t3 )
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount
FROM database2.table
WHERE ids IN (t1.values)
) as t2
WHERE t1.id = 20;
I get an error, that t1.values inside the subquery is unknown column.
You need to rewrite your query and take inne where to join condition:
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount
FROM database2.table
) as t2 ON t2.ids = t1.values
WHERE t1.id = 20;
Also, you don't use amount column, so what is the point of join?
Another issue, you don't have any join condition defined.
I think you need to read about joins in SQL first :)
It seems you are trying to join database2.table to your t1 based on t1.values list.
I added group by IDs in t2 since your using aggregation function. Then, not sure what's the purpose of your sum(amount)
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount, ids
FROM database2.table
GROUP BY ids
) as t2 on t2.ids IN (t1.values)
WHERE t1.id = 20;
Hi i am new to subqueries and so not sure if i am doing right
I am happy with this
SELECT t2.cons, t1.date, t1.account_no FROM
(
SELECT date, account_no FROM tbl_consignment_x3
) t1
INNER JOIN
(
SELECT cons, date, account_no FROM
tbl_volume_analysis
) t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
It gives me the results i want to use
So i was hoping something like this below would do what i want but i can't get the syntax right plus i'm not sure if my technique is completely wrong
UPDATE tbl_margin_all t3
(
SELECT t2.cons, t1.date, t1.account_no FROM
(
SELECT date, account_no FROM tbl_consignment_x3
) t1
INNER JOIN
(
SELECT cons, date, account_no FROM
tbl_volume_analysis
) t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
)
SET t3.cons=t2.cons
WHERE t1.date=t3.date AND t1.account_no=t3.account_no
thanks
ADDITION:
First i do a total count of consignments for a particular date taken from consignment table that is then written in volume table with the date and account.. then from consignments table i want to write to each consignment that count that i did.
You need to join with the first subquery -- put JOIN after t3.
Actually, you don't need any of the subqueries at all, you can just join directly with the tables.
UPDATE tbl_margin_all AS t3
JOIN tbl_consignment_x3 AS t1 ON t1.date = t3.date AND t1.account_no=t3.account_no
JOIN tbl_volume_analysis AS t2 ON t2.account_no=t1.account_no AND t2.date=t1.date
SET t3.cons = t2.cons
It's the same in the SELECT query, it should be:
SELECT t2.cons, t1.date, t1.account_no
FROM tbl_consignment_x3 AS t1
JOIN tbl_volume_analysis AS t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
Notice the comma after t3 for syntax
UPDATE tbl_margin_all_4000 t3,
(
SELECT cons, t1.date, t1.account_no FROM
(
SELECT date, account_no, consignment_no FROM tbl_consignment_x3_4000
) t1
INNER JOIN
(
SELECT cons, date, account_no FROM
tbl_volume_analysis_4000
) t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
) as src
SET t3.cons=src.cons
WHERE t3.date=src.date AND t3.account_no=src.account_no
this query took 4 mins. i had tried the double joins as suggested kindly but that took 45 mins.
I was trying to optimize this join since both tables are large (# rows, # records):
Update Table1 as T1
Inner Join Table2 as T2
On T1.X=T2.Y
Set T1.A=T2.B;
Ended up getting great performance increase doing:
Update Table1 as T1
Inner Join (Select T2.Y,T2.B from Table2) as T2
On T1.X=T2.Y
Set T1.A=T2.B;
So figured I'd do the same for Table1. Yet when I try:
Update (Select T1.X, T1.A from Table1) as T1
Inner Join (Select T2.Y,T2.B from Table2 )as T2
On T1.X=T2.Y
Set T1.A=T2.B;
I get the error that T1 is not updateable. How can I know limit the fields loaded by Table1 as well?
I am trying to delete rows from a table that spit out from a join:
DELETE FROM t1 WHERE company_name IN
(SELECT company_name FROM t1
LEFT OUTER JOIN t2
ON t2.company_name = t1.company_name
WHERE t2.name IS null)
Column 'company_name' in field list is ambiguous
Getting this ambiguous error while trying to make this query? Any suggestions?
MySQL doesn't like it when you try to UPDATE/DELETE a table and SELECT from the same table in the same query.
You can solve this with multi-table DELETE syntax:
DELETE t1 FROM t1 LEFT OUTER JOIN t2 USING (company_name)
WHERE t2.name IS NULL;
As the error message tells the column name company_name is not unique. Depending on your needs I believe this may solve your problem assuming you're trying to delete entries in t1 which doesn't have a corresponding row in t2 or have one but with name is null:
DELETE FROM
t1
WHERE
company_name NOT IN (
SELECT
t2.company_name
FROM
t2
WHERE
t2.name IS NOT NULL
)
Try that
DELETE FROM t1 WHERE company_name IN ( select * from (SELECT t1.company_name FROM t1 LEFT OUTER JOIN t2 ON t2.company_name = t1.company_name WHERE t2.name IS null) t )