There's a table called order_flags that I am trying to join with orders table with orders.id being the foreign key in order_flags as order_fk. All I need is to exclude an order whose foreign key record has value 7 set for 'order_flags.flag' field. The SQL that I am trying below returns empty records. I expect this to return all the records except for the one having 7 as value for flag field in order_flags table. Tables in the question are:
SELECT orders.id,
orders.po_number,
orders.payment_method,
order_flags.flag,
order_details.vendor_fk,
Max(order_details.estimated_shipped_date) AS estimated_shipped_date,
Max(back_orders.back_order_date) AS back_order_date
FROM orders
INNER JOIN order_details
ON order_details.order_fk = orders.id
LEFT JOIN order_flags
ON order_flags.order_fk = orders.id
LEFT JOIN sku
ON sku.item_sku = order_details.sku
LEFT JOIN back_orders
ON back_orders.sku_fk = sku.id
WHERE orders.order_status = 'PL'
AND ( ( order_details.item_status = 'PL' )
OR ( order_details.item_status = 'SN' ) )
AND orders.placed_date >= '1969-07-03'
AND orders.flag_overdue_po = '1'
AND order_details.estimated_shipped_date != '0000-00-00'
AND order_details.vendor_fk NOT IN ( '57', '0', '72', '161' )
AND order_flags.flag != '7'
GROUP BY orders.id,
order_details.vendor_fk
If in a WHERE clause you mention a column from a LEFT JOINed table, you convert the join to INNER JOIN.
You have this.
FROM orders
...
LEFT JOIN order_flags
ON order_flags.order_fk = orders.id
...
WHERE ... order_flags.flag != '7'
Try moving the filter to the ON clause like this.
FROM orders
...
LEFT JOIN order_flags
ON order_flags.order_fk = orders.id
AND order_flags.flag != '7'
...
WHERE ...
Or try this...
WHERE ... (order_flags.flag IS NULL OR order_flags.flag != '7')
Why does this happen? If no row from the left of the LEFT JOIN matches a row from the right, the resultset of the join has NULLs for the columns from the left. NULLs are weird. They're not equal to anything, and they're not unequal to anything. So your WHERE filter eliminated lots of rows.
Related
I have this query, which works fine:
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
So, it's a query on table 1 with a join on table 2, then subsequent joins from multiple aliased joins of table 3 on table 2, but as soon as I add further joins, I get no results and I'm not sure why, for example:
select table_1.*, coalesce(test_1.type, test_2.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
inner join `table_3` as `test_2` on `test_2`.`code` = `table_2`.`column` and `table_2`.`column` = 'H'
Can anyone explain what I have done wrong?
Try LEFT join on table_3 . If there are no records for table_3, that's why you yield no results, due to the INNER join.
And actually, you're not joining any columns on table_3. Is most likely the issue.
What is the expected sample result of the second of your query ?
Could you please try this query ?
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and
(`table_2`.`column` = 'L' or `table_2`.`column` = 'H')
i have two tables vms_vendor_job_submission table as main, and second vms_offer table, main table primary key is foreign key in second table, so i need all those record which have job_id=101 and resume_status=7 from main table and it should have no entry in second table or if entry exist then its status should be 2(rejected).
select s.*
from vms_vendor_job_submission s left join
vms_offer o
on s.id = o.submission_id and
o.status = '2'
where s.job_id=".$_GET['job_id']." and s.resume_status='7'
I have fixed it....and working fine
"SELECT s.* FROM vms_vendor_job_submission s
LEFT JOIN vms_offer o
ON s.id = o.submission_id
WHERE s.job_id = 101 and if(s.id =o.submission_id, o.status = 2 and s.resume_status = '7',s.resume_status = '7')";
I think this is the logic you seem to want:
select s.*
from vms_vendor_job_submission s left join
vms_offer o
on s.id = o.submission_id
where s.job_id=".$_GET['job_id']." and
s.resume_status = '7' and
(o.status is null or o.status = '2')
The following query is confusing to me. It does not show all rows in the products table if I do WHERE (inventory_to_pos.POSID IS NULL OR inventory_to_pos.POSID = ?) and bind a POSID that may or may not exist in inventory_to_pos. When LEFT JOIN-ing a table, should I not get all rows from the original table, when I only filter on the original table and use IS NULL on any conditions for LEFT JOIN'ed tables?
SELECT products.ID,products.NAME,products.VOLUME,productcombinations.PRODUCTID,productcombinations.PART,inventory_to_pos.FULLCOUNT
FROM products
LEFT JOIN productcombinations ON products.ID = productcombinations.PARTOF
LEFT JOIN inventory_to_pos ON products.ID = inventory_to_pos.PRODUCT
WHERE products.INVENTORY = 1
AND products.AVAILABLE = 1
AND products.ID > 0
AND (inventory_to_pos.POSID IS NULL OR inventory_to_pos.POSID = ?);
In the case where inventory_to_pos.PRODUCT and inventory_to_pos.POSID does not exist for the given product and POSID, I am given no rows. Why?
Move all related invetory_to_pos clauses into LEFT JOIN, i.e.:
SELECT
products.ID,
products. NAME,
products.VOLUME,
productcombinations.PRODUCTID,
productcombinations.PART,
inventory_to_pos.FULLCOUNT
FROM
products
LEFT JOIN productcombinations ON products.ID = productcombinations.PARTOF
LEFT JOIN inventory_to_pos ON products.ID = inventory_to_pos.PRODUCT AND (
inventory_to_pos.POSID IS NULL
OR inventory_to_pos.POSID = ?
)
WHERE
products.INVENTORY = 1
AND products.AVAILABLE = 1
AND products.ID > 0
I am attempting to get the sum of 12 columns (in same table) in a subquery in an inner join.
Here is a link to my schema :
SqlFiddle
The query I am attempting to use is this:
SELECT
`inventory`.`part_number`,
`inventory`.`qty`,
`inventory`.`description`,
`reorder`.`reorder_point` * '1' `point`,
`inventory`.`cost`,
`vendor`.`name` AS `vendor_name`, SELECT (SUM(`saleshistory`.`Sales_1_Month_Prior`)+SUM(`saleshistory`.`Sales_2_Month_Prior`)+SUM(`saleshistory`.`Sales_3_Month_Prior`)+SUM(`saleshistory`.`Sales_4_Month_Prior`)+SUM(`saleshistory`.`Sales_5_Month_Prior`)+SUM(`saleshistory`.`Sales_6_Month_Prior`)+SUM(`saleshistory`.`Sales_7_Month_Prior`)+SUM(`saleshistory`.`Sales_8_Month_Prior`)+SUM(`saleshistory`.`Sales_9_Month_Prior`)+SUM(`saleshistory`.`Sales_10_Month_Prior`)+SUM(`saleshistory`.`Sales_11_Month_Prior`)+SUM(`saleshistory`.`Sales_12_Month_Prior`) AS TTL
FROM `inventory`
LEFT JOIN `reorder` ON `inventory`.`part_number` = `reorder`.`part_number`
LEFT JOIN `vendor` ON `inventory`.`vendor` = `vendor`.`vendor_id`
INNER JOIN `saleshistory` ON `saleshistory`.`location` = `inventory`.`location` AND `saleshistory`.`part_number` = `inventory`.`part_number`
WHERE `inventory`.`qty` <= `reorder`.`reorder_point`
AND `inventory`.`location` = '99'
AND `reorder`.`reorder_point` != '0'
GROUP BY `inventory`.`part_number`
ORDER BY `vendor`.`name` ASC
When using this query, it returns all the values for all the records not just the rows.
This is my mysql query!
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
LEFT JOIN invoice
ON invoice.projects_id = projects.projects_id
LEFT JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND (SUM( invoice.invoice_amount) < projects.projects_cost
OR invoice.projects_id is null )
AND project_assign.project_completed_date IS NOT NULL
In this query i want select all row that:
Is not present in other table e.g. (in my case other table is
"invoice")
Or if persent then this condition must hold sum(invoice.invoice_amount) < projects.projects_cost
Thanks.
Divide your problem. Use a UNION. First use one query to select all records that are not present in the first table (Use a LEFT JOIN), union that with the result of your second query which would give you all records meeting your second condition (use an outer join)
//Select all records present in left table
//union
//select all records present in both tables matching your condition.
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
LEFT JOIN invoice
ON invoice.projects_id = projects.projects_id
LEFT JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND project_assign.project_completed_date IS NOT NULL
UNION
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
INNER JOIN invoice
ON invoice.projects_id = projects.projects_id
INNER JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND (SUM( invoice.invoice_amount) < projects.projects_cost
AND project_assign.project_completed_date IS NOT NULL
select projects.projects_id, projects.projects_title,projects.projects_cost
from projects
left join invoice
on invoice.projects_id = projects.projects_id
left join project_assign
on project_assign.projects_id=projects.projects_id
where project_assign.assigned_user_id=3 and
((select sum(invoice.invoice_amount) from invoice) < projects.projects_cost or invoice.projects_id is null )
and project_assign.project_completed_date is not null
You cannot put aggregation functions in the where clause. In this case, you can do the aggregation using a subquery and then do the comparison:
SELECT p.projects_id, p.projects_title, p.projects_cost
FROM projects p LEFT JOIN
(select i.projects_id, sum(invoice_amount) as invoice_amount
from invoice i
) i
ON i.projects_id = p.projects_id LEFT JOIN
project_assign pa
ON pa.projects_id = p.projects_id
WHERE pa.assigned_user_id = 3 AND
(i.invoice_amount < p.projects_cost OR i.projects_id is null ) AND
pa.project_completed_date IS NOT NULL;