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)
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:
SQL query return data from multiple tables
(6 answers)
Closed 6 years ago.
I want to take all the data from two tables with the same user_id, but I don't know what type of join to use and I want to be sure with the syntax of the query.
"users" table:
"schedule" table:
Here's a simple LEFT JOIN example:
SELECT * FROM `users` LEFT JOIN `schedule` ON `users`.`userid` = `schedule`.`user_id`
WHERE `users`.`userid` = ?
this will collect everything from both tables and then you can output it accordingly.
This question already has an answer here:
MySQL JOIN tables with duplicate column names
(1 answer)
Closed 6 years ago.
I am using SELECT SQL_CALC_FOUND_ROWS *, but I need to left join a table that has a column with the same name as the first one.
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
LEFT JOIN $this->_db2 ON $this->_db2.calc_id = $this->_db.calc_id
";
Any idea how to make this work, as now the value from the 2nd table is overwriting the fist one?
Since you are joining a table with a field with the same name as another, you cannot use SELECT *.
You need to manually list the fields that you want in the SELECT statement.