This question already has an answer here:
Insert into one table base on join result from 2 other table
(1 answer)
Closed 3 years ago.
I have an account and customer tables which can be linked by customer col and get the date col to the account table.
account table:
customer table:
final table:
If I use the below query, I am getting following error
ERROR: more than one row returned by a subquery used as an expression
What am I doing wrong?
UPDATE account
SET date = (
SELECT date
FROM customer
WHERE customer.customer = account.customer
);
Consider the update ... join ... set syntax:
update account a
inner join customer c on a.customer = c.custom
set a.date = c.date
Related
This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 4 years ago.
I've created a new column (third one) and all records are null in that column. The table has 100 rows. When I try INSERT INTO, records are added but only from 101 row and previous ones are still null. That's why I decided to use UPDATE, but I have the next error message: You can't specify target table 'actors' for update in FROM clause
Here is my query:
UPDATE
actors
SET
starred_count = (
SELECT COUNT(actors.Actor_id)
FROM starred
INNER JOIN actors ON actors.Actor_id = starred.Actor_id
GROUP BY starred.Actor_id
ORDER BY starred.Actor_id
)
How could I do this properly?
Give a try to next query, since on MySQL updating using a query is not valid.
UPDATE
actors AS a
INNER JOIN
(SELECT Actor_id, COUNT(*) AS starred_count
FROM starred
GROUP BY Actor_id) AS s ON s.Actor_id = a.Actor_id
SET
a.starred_count = s.starred_count
This question already has answers here:
MYSQL Left Join how do I select NULL values?
(4 answers)
Closed 4 years ago.
I'm developing a system and creating invoices. I want the record where no invoice has been created.
I'm trying to write a MySQL query, where I need the records whch can not be joined with another table. In other words, the records that do not have a linked record in the other table.
I tried the following query
SELECT exports.id as e_id,export_invoices.id as i_id
FROM exports
LEFT JOIN export_invoices ON export_invoices.export_id = exports.id
and got this result:
Which gives all value and also the record of which invoice is not created with NULL value (I want to have that [e_id->2 from result]). I just want to extract that null value record's master id.
Simply add the where condition in your query -
SELECT exports.id as e_id,export_invoices.id as i_id
FROM exports LEFT JOIN export_invoices on export_invoices.export_id = exports.id
WHERE export_invoices.id IS NULL;
This question already has answers here:
MySQL, update multiple tables with one query
(7 answers)
Closed 4 years ago.
I have to pass a mysqli update query. Following are the three different tables with the situation.
I have a user_table with columns
user_id
user_email
I have a webpage_table with columns
webpage_id
first_webpage
second_webpage
I have a user_data_table with only columns
user_id
webpage_id as foreign key
Now I have to update the values of webpage_table where the user_email="johndoe#xyz.com"....
What will be my update mysqli_query() for the above situation...I tried but i'm not able to go any further of where condition.... Below is my attempt
UPDATE `webpage_table` SET `first_webpage`='xyz', `second_webpage`='xyz'
WHERE
You could use an updated based on join
with the related table
UPDATE `webpage_table`
INNER JOIN user_data_table d on u.user_id = d.user_id
and `webpage_table`.webpage_id = d.webpage_id
INNER JOIN user_table u.user_email="johndoe#xyz.com"
SET `first_webpage`='xyz',
`second_webpage`='xyz'
This question already has answers here:
String concatenation in MySQL
(5 answers)
Closed 6 years ago.
I added a column to my WorkSheetTransaction table and want to populate it with a name built from the Department table. Both tables are already populated with the join field DepartmentId.
The following query runs ok but no rows are updated. Why not?
update WorkSheetTransactions
inner join Departments on WorkSheetTransactions.DepartmentId = Departments.DepartmentId
set WorkSheetTransactions.DepartmentName = (Departments.GL_Account + '-' + Departments.DepartmentName)
I've tried many variations but I just can't see where I've gone wrong. BTW, the join field is an integer in both tables and all the other 2 fields are var_chars.
In mysql, you should use concat to concatenate strings:
UPDATE WorkSheetTransactions
INNER JOIN Departments
ON WorkSheetTransactions.DepartmentId = Departments.DepartmentId
SET WorkSheetTransactions.DepartmentName = concat(Departments.GL_Account, '-', Departments.DepartmentName)
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I have an inbox of messages for my users, I have a 'messages' table and a 'users' table. Messages table has to and from fields which contain the user IDs.
I want to select the latest message from every user, where the to field is the current user ID, i.e.
"select (latest Message, by Message.ID) from (unique users) where Message.to = $currentUserID (and left join User where UserID = Message.from)"
I want to end up with something like this:
http://www.innerfence.com/blog/wp-content/uploads/screenshot-iphone-inbox-thumb.png
I can't figure out the query I need for this, please help..!
Try this. It is tested and working fine. Updated to include information about the sender.
SELECT * FROM `messages` tm LEFT JOIN `users` tu ON `tm.from` = `tu.userid`
WHERE `tm.date` IN
(SELECT MAX(`date`) FROM `messages` WHERE `to` = $currentUserID GROUP BY `from`);