I have 2 tables that need updating based on 2 where clauses
i included a 3rd table which would join the other 2 tables together. i cant get either working.
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000, list.transfers = list.transfers+1
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Without the 3rd table having an update it would be
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Can anyone help me get this working?
You can change your query to be like below using update-join syntax but I don't see why you need to JOIN with other tables. Your UPDATE statements could be single or individual update as well
UPDATE list,Players,users
INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID
INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET Players.Players_Team_ID = 6,
Users.users_bank = users_bank - 15000000,
list.transfers = list.transfers + 1
WHERE Users.User_ID=14
AND Players.Players_ID=3;
Related
I'm trying to set a post to trash based on the title from another column in another table. Below is the code I have been trying to use, but so far the solution escapes me:
UPDATE egDuqUe_5_posts
SET egDuqUe_5_posts.post_status = 'trash'
FROM
egDuqUe_5_posts
INNER JOIN
egDuqUe_7_posts.id = egDuqUe_7_wc_order_coupon_lookup.coupon_id
WHERE
egDuqUe_5_posts.post_title = egDuqUe_7_posts.post_title
After a cup of tea, I came to this solution:
UPDATE egDuqUe_5_posts m
JOIN
egDuqUe_7_posts s
ON m.post_title = s.post_title
JOIN
egDuqUe_7_wc_order_coupon_lookup c
ON c.coupon_id = s.ID
SET m.post_status = 'trash'
WHERE m.post_type ='shop_coupon' && m.post_title = s.post_title
I am having 3 tables named customers,cars,carrent. All i want is to multiply rentorder.days with cardetail.rentday and show the value in rentorder.totalrent. I am unable to achieve this. How can i do this.
Any suggestions please.
SQL
SELECT customers.*,
cardetail.carname,
cardetail.model,
cardetail.company,
cardetail.color,
cardetail.rentday,
rentorder.days,
rentorder.totalrent
FROM rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
Just multiply rentday with days to get the computed value totalrent in Select list.
Try this
SELECT customers.*, -- Not sure this is allowed in [Mysql]
cardetail.carname,
cardetail.model,
cardetail.company,
cardetail.color,
cardetail.rentday,
rentorder.days,
cardetail.rentday * rentorder.days AS totalrent
FROM rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
To save the data you need to use update from Inner Join syntax
update rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
SET
rentorder.totalrent = cardetail.rentday * rentorder.days
So my database is composed of 5 tables with different columns for each one. The only column that keeps them all identified is the id. I'm trying to get the data for a specific user, but I only seem to get all users of the database instead.
This is what I have tried:
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM
MatrixUsers.UserInfo
INNER JOIN
MatrixUsers.Users
ON
UserInfo.idUserInfo = Users.idUsers = 1
INNER JOIN
MatrixUsers.ControlAccess
ON
ControlAccess.idControlAccess = UserInfo.idUserInfo = 1
INNER JOIN
MatrixUsers.Sessions
ON
Sessions.idSessions = ControlAccess.idControlAccess = 1
INNER JOIN
MatrixUsers.FilePath
ON
FilePath.idFilePath = Sessions.idSessions = 1
INNER JOIN
MatrixUsers.Operations
ON
Operations.idOperations = FilePath.idFilePath = 1;
I tried putting 1 at the end of each id to see if they matched, but I still get all the users.
I'm new to SQL and I'm only familiar with matching rows, but not choosing specific one.
Here are the columns of each table:
ControlAccess: {idControlAccess, UserName, Pass}
Sessions: {idSessions, Matrix1, Matrix2, Result}
FilePath: {idFilePath, LocationFiles}
Operations: {idOperation, Operation}
UserInfo: {idUserInfo, Age, Country, Address, ZipCode, Phone}
Use WHERE when you want specific user. for example, select user_id from table where user_id=the_specific_user_id . Follow this basic to built you complicate statement.
Just user where after all the joins
WHERE ANY_COLUMN_REFER_TO_USER_ID = YOUR_NEEDED_ID
so your full query would be like :
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM MatrixUsers.UserInfo
INNER JOIN MatrixUsers.Users
ON UserInfo.idUserInfo = Users.idUsers
INNER JOIN MatrixUsers.ControlAccess
ON ControlAccess.idControlAccess = UserInfo.idUserInfo
INNER JOIN MatrixUsers.Sessions
ON Sessions.idSessions = ControlAccess.idControlAccess
INNER JOIN MatrixUsers.FilePath
ON FilePath.idFilePath = Sessions.idSessions
INNER JOIN MatrixUsers.Operations
ON Operations.idOperations = FilePath.idFilePath
WHERE UserInfo.idUserInfo = 1
I have this SQL which updates rows on MySQL, but I need to rewrite it so that it will work on PostgreSQL as well. I'm aware that I can make it work for PostgreSQL using a different syntax, but I need one statement which will work for both systems.
UPDATE {coursework_feedbacks} AS f
INNER JOIN {coursework_submissions} AS s
ON f.submissionid = s.id
INNER JOIN {coursework} c
ON s.courseworkid = c.id
SET f.stage_identifier = 'assessor_1'
WHERE f.ismoderation = 0
AND f.isfinalgrade = 1
AND c.numberofmarkers = 1
As this particular case can be phrased with a correlated subquery, you can just write:
UPDATE {coursework_feedbacks} AS f
SET f.stage_identifier = 'assessor_1'
WHERE f.ismoderation = 0
AND f.isfinalgrade = 1
AND EXISTS (
SELECT 1
FROM {coursework_submissions} AS s
INNER JOIN {coursework} c ON s.courseworkid = c.id
WHERE f.submissionid = s.id AND c.numberofmarkers = 1
);
but in the more general case where that isn't true (say, where you need to update a value based on the result of the join) I don't know if there is a portable, efficient way.
I am having 4 tables.
I want to update all table for that i am using joins.
Query is working only if all tables have values. If any one table does not have record for that than it is not updating other tables
MY query is
update post_message_users
LEFT JOIN post_messages
ON post_message_users.messageid = post_messages.messageid
LEFT JOIN comments
ON post_message_users.messageid = comments.comment_on
LEFT JOIN likes
ON post_message_users.messageid =likes.element_id
SET post_message_users.status='deleted',
post_message_users.deleted_time=NOW(),
post_messages.status = 'deleted' ,
post_messages.delete_time = now(),
comments.status ='deleted',
likes.status='deleted',
likes.delete_time=now()
WHERE
comments.element_type ='Message'
AND
likes.element_type ='Message'
and post_message_users.messageid = 33
and post_message_users.received_by= 3
Please see what change should I make so that i work properly.
Your where clause is undoing the left outer join, turning it into an inner join. To fix this, move the conditions into the on clause:
update post_message_users
LEFT JOIN post_messages
ON post_message_users.messageid = post_messages.messageid
LEFT JOIN comments
ON post_message_users.messageid = comments.comment_on and
comments.element_type = 'Message'
LEFT JOIN likes
ON post_message_users.messageid = likes.element_id and
likes.element_type = 'Message'
SET post_message_users.status='deleted',
post_message_users.deleted_time=NOW(),
post_messages.status = 'deleted' ,
post_messages.delete_time = now(),
comments.status ='deleted',
likes.status='deleted',
likes.delete_time=now()
WHERE post_message_users.messageid = 33 and post_message_users.received_by= 3;