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
Related
I am having issues with getting this double left join to get the listingspecificsListPrice, but that info exists in the table, cant figure out why it would not include it. This is my sql.
SELECT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID AND mls_images.imgOrder = 0
WHERE userID = 413
GROUP BY mls_subject_property.mls_listingID
The result comes out like this..
All of the other fields come back, but it doesnt seem to want to bring back those two items.
This is a picture of the other table, to show that the data does in fact exist.
The mls_images.imgOrder = 0 condition should be in the join with mls_images, not mls_forms_listing_specifics.
Don't use GROUP BY if you're not using any aggregation functions. Use SELECT DISTINCT to prevent duplicates.
SELECT DISTINCT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID AND mls_images.imgOrder = 0
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID
WHERE userID = 413
The following query returns what is displayed in the attached image.
What I thought would be very simple has turned out to be very complex. All I simply want to do is now total up the result into one row and column. In this case the sum would be 161. How do I do this? I've literally tried everything. I hope I've provided enough information.
SELECT
TRUNCATE
(
SUM(
`assignment`.`percentage_achieved` *(
SELECT
`unitComponentWeighting`.`percentage_weighting` / 100
FROM
`unitComponentWeighting`
WHERE
`assignment`.`assignment_component_id` = `unitComponentWeighting`.`component_lookup_id`
LIMIT 1
)
),
2
) AS `unit_percentage_grade`
FROM
`assignment`
LEFT JOIN `assignmentType` ON `assignment`.`assignment_type_id` = `assignmentType`.`id`
LEFT JOIN `assignmentComponentLookup` ON `assignmentComponentLookup`.`id` = `assignment`.`assignment_component_id`
LEFT JOIN `unit` ON `unit`.`id` = `assignment`.`unit_id`
LEFT JOIN `assignmentSequence` ON `assignmentSequence`.`id` = `assignment`.`assignment_sequence_id`
LEFT JOIN `yearGroup` ON `yearGroup`.`id` = `unit`.`year_group_id`
WHERE
`yearGroup`.`id` = 1
GROUP BY
`assignment`.`unit_id`
try removing this part
" GROUP BY
assignment.unit_id "
I'm wanting to do the following:
Select everything from product that matches the value of type_id from both tables product and system_type. Then with those matched results, match cat_id from both tables system_type and system_cat and then refine the final result where cat_type = 0 from the system_cat table.
Current SQL seems to have a syntax error:
SELECT * FROM product
JOIN system_type
USING (type_id)
JOIN system_cat
USING (cat_id)
WHERE cat_type = 0
What else I've tried:
SELECT * FROM product
JOIN system_type
USING system_type.type_id = product.type_id
JOIN system_cat
USING system_type.cat_id = system_cat.cat_id
WHERE system_cat.cat_type = 0
Try this. You may need to explicitely type out the columns you need
SELECT * FROM product as pr
INNER JOIN system_type as st
ON st.type_id = pr_id
INNER JOIN system_cat as sc
ONH st.cat_id = sc.cat_id
WHERE sc.cat_type = 0
Syntax changes when using table_name.column method. Use ON and not USING.
SELECT * FROM product
JOIN system_type
ON system_type.type_id = product.type_id
JOIN system_cat
ON system_type.cat_id = system_cat.cat_id
WHERE system_cat.cat_type = 0
I have two tables. I want to left join with one of the tables based on certain condition. I want to do something like this:
IF (Projects.paymentType==1)
LEFT JOIN ProjectBudgetFixedPrice ON (Projects.budget=ProjectBudgetFixedPrice.id)
ELSE
LEFT JOIN ProjectBudgetPerHourPrice ON (Projects.budget=ProjectBudgetPerHourPrice.id)
I have created the following, but it is not working.
SELECT * FROM Projects LEFT JOIN
(IF (Projects.paymentType==1,ProjectBudgetFixedPrice,ProjectBudgetPerHourPrice))
ON
(IF (Projects.paymentType==1,Projects.budget=ProjectBudgetFixedPrice.id,Projects.budget=ProjectBudgetFixedPrice.id))
WHERE (Projects.statusCode=3 OR Projects.statusCode=4) AND Projects.suspended='N' AND (Projects.paymentType=1 OR Projects.paymentType=2)
Any help or suggestions please?
You should be able to use a UNION.
Something like
SELECT
*
FROM
Projects
LEFT JOIN
ProjectBudgetFixedPrice
ON Projects.budget = ProjectBudgetFixedPrice.id
WHERE
Projects.paymentType = 1
AND ((Projects.statusCode = 3 OR Projects.statusCode = 4)
AND Projects.suspended = 'N'
AND (Projects.paymentType = 1 OR Projects.paymentType = 2))
UNION
SELECT
*
FROM
Projects
LEFT JOIN
ProjectBudgetPerHourPrice
ON Projects.budget = ProjectBudgetPerHourPrice.id
WHERE
Projects.paymentType <> 1
AND ((Projects.statusCode = 3 OR Projects.statusCode = 4)
AND Projects.suspended = 'N'
AND (Projects.paymentType = 1 OR Projects.paymentType = 2));
HTH
I have written a following query.
UPDATE
tbl_bookings tb
INNER JOIN
tbl_slots ts
ON ( tb.slot_id = ts.id )
SET tb.seat_freed = 1, ts.free_machines = ts.free_machines + 1
WHERE 1
AND tb.seat_freed = 0
AND tb.transactionComplete = 0
Here I am trying to free the seats by updating the seat_freed to 1 and increasing the free_machines counter by 1.
In case, there are more than 1 rows (say 3 rows) returned from tbl_bookings, I would want to increment the counter by .
Is there any way to do it, using the single. I can obviously do it by breaking it down into different queries, but single query is what I desire. :)
You could use a subquery with the exact same conditions to calculat the number of rows which will be affected by the update. I used DISTINCT for the count since i don't know how bookings and slots are related in your example.
UPDATE tbl_bookings tb
INNER JOIN tbl_slots ts ON ( tb.slot_id = ts.id )
INNER JOIN (SELECT count(DISTINCT b.id) seats_to_be_freed
FROM tbl_bookings b INNER JOIN tbl_slots s ON ( b.slot_id = s.id )
WHERE b.seat_freed=0 and b.transactionComplete=0) tmp
SET tb.seat_freed = 1, ts.free_machines = tmp.seats_to_be_freed
WHERE 1
AND tb.seat_freed = 0
AND tb.transactionComplete = 0