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;
Related
I've got a script where checked in employee's can see the stock inventory from each other, linked with their personal stock location, so each checked in employee can see which items are in stock at different locations. However, I want the main stock (id of 1, which is not attached to an employee) to be showed always, but I can't get the query right because one of the where statements is clearly not correct:
`stock_locations`.`location_id` = 1 AND
`workschedule`.`checkedIn` = 1 AND
Rememeber, the main stock is not linked to an employee, so it doesn't show up at the workschedule table. If I remove the first statement, It clearly shows up all the checked in employee's with their location, but that doesn't give me the main stock. If I remove the second statement, it only shows me the main stock.
How can I solve this issue within SQL? This is btw the full statement:
SELECT
`item_quantities`.`item_id`,
`stock_locations`.`location_name`,
`item_quantities`.`quantity`,
`people`.`first_name`
FROM
`item_quantities`
JOIN `stock_locations` ON `item_quantities`.`location_id` = `stock_locations`.`location_id`
JOIN `items` ON `item_quantities`.`item_id` = `items`.`item_id`
LEFT JOIN `workschedule` ON `workschedule`.`linked_storage` = `stock_locations`.`location_id`
LEFT JOIN `people` ON `workschedule`.`employee_id` = `people`.`person_id`
WHERE
`stock_locations`.`location_id` = 1 AND
`workschedule`.`checkedIn` = 0 AND
`items`.`unit_price` != 0 AND
`items`.`deleted` = 0 AND
`stock_locations`.`deleted` = 0 NULL
Thanks in advance!
Make it an OR statement inside of parens.
(`stock_locations`.`location_id` = 1 OR `workschedule`.`checkedIn` = 1) AND
This will return all records that match either the main stock or the employee.
You need to use the OR operator. Clearly both things can't happen at the same time, so you need to specify each set of acceptable conditions.
SELECT
`item_quantities`.`item_id`,
`stock_locations`.`location_name`,
`item_quantities`.`quantity`,
`people`.`first_name`
FROM
`item_quantities`
JOIN `stock_locations`
ON `item_quantities`.`location_id` = `stock_locations`.`location_id`
JOIN `items`
ON `item_quantities`.`item_id` = `items`.`item_id`
LEFT JOIN `workschedule`
ON `workschedule`.`linked_storage` = `stock_locations`.`location_id`
LEFT JOIN `people`
ON `workschedule`.`employee_id` = `people`.`person_id`
WHERE
`stock_locations`.`location_id` = 1
OR (
AND `workschedule`.`checkedIn` = 1
AND `items`.`unit_price` != 0
AND `items`.`deleted` = 0
AND `stock_locations`.`deleted` = 0
NULL
)
You have LEFT JOIN, but your WHERE clause turns them into inner joins. Fixing that will probably fix your problem:
SELECT . . .
FROM item_quantities iq JOIN
stock_locations sl
ON iq.`location_id` = sl.`location_id` JOIN
items i
ON iq.`item_id` = i.`item_id` LEFT JOIN
workschedule ws
ON ws.`linked_storage` = sl.`location_id` AND
ws.`checkedIn` = 0 LEFT JOIN
--------^
people p
ON ws.`employee_id` = p.`person_id`
WHERE sl.`location_id` = 1 AND
i.`unit_price` != 0 AND
i.`deleted` = 0 AND
sl.`deleted` = 0
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;
I have a SQL query that left joins a few tables in different ways depending on a conditions.
SELECT
dh_partner.company_name,
dh_partner_abonnement.name,
dh_partner_abonnement.description,
dh_partner_abonnement.price,
dh_partner_abonnement.discount_price,
dh_studio.partner_id,
CONCAT('https://some.url/images/studio/logo/', dh_studio.logo) as logo_url,
CONCAT('https://some.url/studio/', dh_studio.alias) as page_url,
CONCAT('https://some.url/order/abonnement/', dh_studio.alias, '/', dh_partner_abonnement_studios.abonnement_id) as checkout_url
FROM dh_partner_abonnement
LEFT JOIN dh_partner on dh_partner.id = dh_partner_abonnement.partner_id
LEFT JOIN dh_studio on dh_studio.partner_id = dh_partner.id
LEFT JOIN dh_partner_abonnement_studios on dh_partner_abonnement_studios.studio_id = dh_studio.id
WHERE
dh_partner.status = 'active'
and dh_partner.id = dh_studio.partner_id
and dh_partner.city_id = '1'
and dh_partner_abonnement_studios.studio_id = dh_studio.id
and dh_studio.show_status = '1'
The challenge in the following code
CONCAT('https://some.url/ru/order/abonnement/', dh_studio.alias, '/', dh_partner_abonnement_studios.abonnement_id) as checkout_url
LEFT JOIN dh_partner_abonnement_studios on dh_partner_abonnement_studios.studio_id = dh_studio.id
and dh_partner_abonnement_studios.studio_id = dh_studio.id
It repeats to the each dh_partner_abonnement this column dh_partner_abonnement_studios.abonnement_id
I have to connect them correctly. I know that I need to use if, however having no idea how.
Just guessing from the table names:
SELECT
p.company_name,
pa.name,
pa.description,
pa.price,
pa.discount_price,
s.partner_id,
CONCAT('https://some.url/images/studio/logo/', s.logo) as logo_url,
CONCAT('https://some.url/studio/', s.alias) as page_url,
CONCAT('https://some.url/order/abonnement/', s.alias, '/', pa.abonnement_id)
as checkout_url
FROM dh_partner_abonnement pa
JOIN dh_partner_abonnement_studios pas ON pas.partner_id = pa.partner_id
AND pas.abonnement_id = pa.abonnement_id
JOIN dh_studio s ON s.id = pas.studio_id
AND s.show_status = 1
JOIN dh_partner p ON p.id = pa.partner_id
AND p.status = 'active'
AND p.city_id = 1
You haven't joined dh_partner_abonnement and dh_partner_abonnement_studios on the complete key, which should include the abonnement_id.
It also seems a studio has a partner_id to indicate that it belongs to a certain partner. However, in your query you are interested in the abbonement studios, so join the studio on the abbonement's studio_ids.
I have the following query
SELECT custconcompany, custconfirstname, custconlastname, custconemail, custconphone, shipaddress1, shipaddress2, shipcity, stateabbrv, shipzip, countryname, websitecheck.formfieldfieldvalue websitevalue, excludecheck.formfieldfieldvalue excludevalue
FROM obcisc_customers
JOIN ( (obcisc_shipping_addresses JOIN obcisc_countries
ON obcisc_shipping_addresses.shipcountryid = obcisc_countries.countryid)
LEFT JOIN obcisc_country_states
ON obcisc_shipping_addresses.shipstateid = obcisc_country_states.stateid
LEFT JOIN obcisc_formfieldsessions websitecheck
ON obcisc_shipping_addresses.shipformsessionid = websitecheck.formfieldsessioniformsessionid
LEFT JOIN obcisc_formfieldsessions excludecheck
ON obcisc_shipping_addresses.shipformsessionid = excludecheck.formfieldsessioniformsessionid)
ON obcisc_customers.customerid = obcisc_shipping_addresses.shipcustomerid
WHERE custgroupid = 11
AND websitecheck.formfieldfieldid = 24
AND excludecheck.formfieldfieldid = 30
AND excludecheck.formfieldfieldvalue != 'a:1:{i:0;s:3:"Yes";}'
ORDER BY shipstate, shipcity
This works great except I also need it to return rows where "excludecheck.formfieldfieldid=30" does not exist... right now it's not returning them
When writing a LEFT JOIN, any criteria on the table you're joining with should be put into the ON clause. If you put it into the WHERE clause, you'll filter out the results in the first table that don't have a matching row in the second table, because the NULL value that comes from the outer join will not match the criteria.
SELECT custconcompany, custconfirstname, custconlastname, custconemail, custconphone, shipaddress1, shipaddress2, shipcity, stateabbrv, shipzip, countryname, websitecheck.formfieldfieldvalue websitevalue, excludecheck.formfieldfieldvalue excludevalue
FROM obcisc_customers
JOIN obcisc_shipping_addresses
ON obcisc_customers.customerid = obcisc_shipping_addresses.shipcustomerid
JOIN obcisc_countries
ON obcisc_shipping_addresses.shipcountryid = obcisc_countries.countryid)
LEFT JOIN obcisc_country_states
ON obcisc_shipping_addresses.shipstateid = obcisc_country_states.stateid
LEFT JOIN obcisc_formfieldsessions websitecheck
ON obcisc_shipping_addresses.shipformsessionid = websitecheck.formfieldsessioniformsessionid
AND websitecheck.formfieldfieldid = 24
LEFT JOIN obcisc_formfieldsessions excludecheck
ON obcisc_shipping_addresses.shipformsessionid = excludecheck.formfieldsessioniformsessionid
AND excludecheck.formfieldfieldid = 30
AND excludecheck.formfieldfieldvalue != 'a:1:{i:0;s:3:"Yes";}')
WHERE custgroupid = 11
ORDER BY shipstate, shipcity
Another way to do it is by putting (excludecheck.formfieldfieldid = 30 OR excludecheck.formfieldfieldid IS NULL) in the WHERE clause. But this is more verbose and also I believe it's harder for MySQL to optimize, especially if you have several tables you're joining like this.
I have 1 table that contains the main rows and i want all of these rows. Then there are 2 tables that may not have a matching row, but i still want the row from the main table if there is no matching row for these tables.
This query works, but if there is not a row in all 3 tables nothing gets returned. Im using mysql with php.
SELECT PS_Info.* , PS_Guides.Guide, PS_User.UserID FROM PS_Info
JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID
JOIN PS_User ON PS_Info.ID = PS_User.InfoID
WHERE PS_Info.GameID = :ID AND PS_User.UserID = :UserID
I am trying to display a list which is in PS_Info. Then i want to hide some of these items if the user has them set to hidden (PS_User = UserID, InfoID). If there is nothing in PS_User then they have not set this item to be hidden so i want to display it. PS_Guides contains extra info about each of the items in the list. Not all items contain extra info so if there is no row in this table then i dont display it.
SELECT PS_Info.* , PS_Guides.Guide, PS_User.UserID FROM PS_Info
left JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID //use left join
left JOIN PS_User ON PS_Info.ID = PS_User.InfoID //use left join
WHERE PS_Info.GameID = :ID AND PS_User.UserID = :UserID
You are looking for 'left join'.
SELECT PS_Info.* , PS_Guides.Guide, PS_User.UserID FROM PS_Info
LEFT JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID
LEFT JOIN PS_User ON PS_Info.ID = PS_User.InfoID
WHERE PS_Info.GameID = :ID AND PS_User.UserID = :UserID