Got a question to ask. How do i get to list all of my left table to display the data when i use the left join even when some row got null value in it?
Heres my query:
SELECT *, SUM(transactions.debit_credit) current_spending
FROM (`budget`) LEFT JOIN `categories` ON `budget`.`category`=`categories`.`idcategory`
LEFT JOIN `user_category` ON `budget`.`category`=`user_category`.`idcategory`
LEFT JOIN `category_transaction` ON `budget`.`category`=`category_transaction`.`idcategory`
LEFT JOIN `transactions` ON `category_transaction`.`idtransaction`=`transactions`.`idtransaction`
WHERE `user_category`.`iduser` = '1'
AND MONTH(transactions.transaction_date) = '11'
GROUP BY `budget`.`idbudget`;
When I remove AND MONTH(transactions.transaction_date) = '11' the query works fine accept it displays all data instead of just 1 month of transaction.
Help?? Please?? Any idea how??
Your where clause turns your left join into an inner join because you filter the data on a condition of a joined table.
Instead put that condition in the on clause of your join
SELECT *, SUM(transactions.debit_credit) current_spending
FROM (`budget`)
LEFT JOIN `categories` ON `budget`.`category`=`categories`.`idcategory`
LEFT JOIN `user_category` ON `budget`.`category`=`user_category`.`idcategory`
AND `user_category`.`iduser` = '1'
LEFT JOIN `category_transaction` ON `budget`.`category`=`category_transaction`.`idcategory`
LEFT JOIN `transactions` ON `category_transaction`.`idtransaction`=`transactions`.`idtransaction`
AND MONTH(transactions.transaction_date) = '11'
GROUP BY `budget`.`idbudget`;
Related
I have a query that contains a sub-query and many Left Outer Joins. Within my sub-query, I am ordering by records by the most recent blog_date and limiting the results by 10 records. This sub-query should dictate the order of the records with the other joins matching up additional information about that record, however, when I add in the LEFT OUTER JOIN it disregards the blog_date ordering. This has led me to believe that I am either missing a key element to persisting the ordering or that LEFT OUTER JOIN is not the right join to be using.
Here is my full query:
SELECT `b`.`blog_id`, `b`.`blog_date`,`b`.`title`, `u`.`user_id`, `u`.`first_name`, `c`.`category_name`, `d`.`discovery_source_name`, `bc`.`comment`, `bf`.`file`
FROM (SELECT * FROM `blog` ORDER BY `blog`.`blog_date` DESC limit 10) `b`
LEFT OUTER JOIN `user` `u` ON `b`.`user_id` = `u`.`user_id` AND `u`.`organization_id` = 1
LEFT OUTER JOIN `category` `c` ON `b`.`category_id` = `c`.`category_id`
LEFT OUTER JOIN `discovery_source` `d` ON `b`.`discovery_source_id` = `d`.`discovery_source_id`
LEFT OUTER JOIN `blog_comment` `bc` ON `b`.`blog_id` = `bc`.`blog_id`
LEFT OUTER JOIN `blog_file` `bf` ON `b`.`blog_id` = `bf`.`blog_id`
;
Here are the results when I just include the first join (user) the records are in the correct order (2017-02-21 most recent):
However, when I add in the second left outer join (and remaining) it appears that the new order is in descending blog_date order, but then grouped by category_name.
Ordering the subquery has no effect on the order of your outer query. Put the ORDER BY at the end of the outer query.
SELECT `b`.`blog_id`, `b`.`blog_date`,`b`.`title`, `u`.`user_id`, `u`.`first_name`, `c`.`category_name`, `d`.`discovery_source_name`, `bc`.`comment`, `bf`.`file`
FROM (SELECT * FROM `blog` ORDER BY `blog`.`blog_date` DESC limit 10) `b`
LEFT OUTER JOIN `user` `u` ON `b`.`user_id` = `u`.`user_id` AND `u`.`organization_id` = 1
LEFT OUTER JOIN `category` `c` ON `b`.`category_id` = `c`.`category_id`
LEFT OUTER JOIN `discovery_source` `d` ON `b`.`discovery_source_id` = `d`.`discovery_source_id`
LEFT OUTER JOIN `blog_comment` `bc` ON `b`.`blog_id` = `bc`.`blog_id`
LEFT OUTER JOIN `blog_file` `bf` ON `b`.`blog_id` = `bf`.`blog_id`
ORDER BY `b`.`blog_date` DESC ;
I have issues with this query. I think the issue is with not finding the items. Afterwards I am doing LEFT OUTER JOIN on it but before that nothing. What is the best solution to include the items table in the beginning ?
SELECT COUNT(*) AS `numrows` FROM (`categories_items`)
LEFT OUTER JOIN `items_stones` `items_stones` ON `items`.`id` = `items_stones`.`item_model_id`
LEFT OUTER JOIN `items` ON `items`.`id` = `categories_items`.`item_model_id`
WHERE ( `items_stones`.`stone_model_id` = 1 ) AND `categories_items`.`category_model_id` = 1
Change your query to be like below, basically swap the LEFT JOINS
SELECT COUNT(*) AS `numrows`
FROM `categories_items`
LEFT OUTER JOIN `items` ON `items`.`id` = `categories_items`.`item_model_id`
AND `categories_items`.`category_model_id` = 1
LEFT OUTER JOIN `items_stones` ON `items`.`id` = `items_stones`.`item_model_id`
AND `items_stones`.`stone_model_id` = 1;
Here is my sql data fiddler http://sqlfiddle.com/#!2/63178/1. Waht's wrong with my query?
SELECT DISTINCT curr.id,curr.curr_tittle, curr.curr_desc
FROM wp_curriculum curr LEFT JOIN (SELECT DISTINCT * FROM wp_curriculum_topic WHERE curr_topic IN (4,12)) AS A ON A.curr_id = curr.id ORDER BY A.id
If you are looking for matching row from both the table then just replace LEFT JOIN to INNER JOIN, otherwise your sql query is showing expected result for LEFT JOIN condition.
SQL Query with INNER JOIN:
SELECT DISTINCT curr.id,curr.curr_tittle, curr.curr_desc FROM wp_curriculum curr INNER JOIN (SELECT DISTINCT * FROM wp_curriculum_topic WHERE curr_topic IN (4,12)) AS A ON curr.id = A.curr_id ORDER BY A.id
Your query works as expected. Could it be you are mixing ID and CURR_ID?
I am trying to work out why the following two queries return different results:
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems` it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
and
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
Obviously the difference is the inner join here, but I don't understand why the one with the inner join is returning less results than the one without it, I would have thought since I didn't do any cross table references they should return the same results.
The final query I am working towards is
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems` it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE cf.`fieldid` =5
AND cf.`value`
REGEXP '[A-Za-z]'
AND i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
But because of the different results that seem incorrect when I add the inner join (it removes some results that should be valid) it's not working at present, thanks.
INNER JOIN statement will retrieve rows that are stored in both table of the jion statement.
Try a LEFT JOIN statement. This will return rows that are in first table but not necessary in the second one :
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
LEFT JOIN `tblinvoiceitems` it ON it.userid=i.userid
LEFT JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
INNER JOIN means show only records where the same ID value exists in both tables.
LEFT JOIN means to show all records from left table (i.e. the one that precedes in SQL statement) regardless of the existance of matching records in the right table.
Try LEFT Join instead of INNER JOIN
SELECT DISTINCT i.id, i.date
FROM `tblinvoices` i
LEFT JOIN `tblinvoiceitems` it ON it.userid=i.userid
LEFT JOIN `tblcustomfieldsvalues` cf ON it.relid=cf.relid
WHERE i.`tax` = 0
AND i.`date` BETWEEN '2012-07-01' AND '2012-09-31'
I've got two tables (let's say "left" and "right" tables). The right table's got from none to several rows matching each row on the left table.
I need to perform a query where I can join the two tables the same way a LEFT OUTER JOIN works, but getting only one row for each existing one in the left table. That row should be the one corresponding to the highest id on the right table.
A NATURAL JOIN would work, but I won't be getting the rows from the left table that don't match any row on the right one.
Try this:
Select L.[ValuesfromLeftTable], ...
R.[ValuesfromRightTable], ...
From LeftTable as L
Left Join RightTable as R
On R.Id = (Select Max(id)
From RightTable
Where FK = L.Id)
This is what I would do:
select *
from `left` as l
LEFT JOIN `right` as r
on (l.id = r.left_id)
group by l.id
order by r.id
You can use GROUP BY clause to show only distinct records
SELECT Left.Field1, Left.Field2, ... Right.Field1, Right.Field2, ...
FROM tblLeft AS Left
LEFT JOIN (SELECT CommonID, Max([ID]) AS MaxID
FROM tblRight
GROUP BY CommonID) AS RightMax ON Left.CommonID = RightMax.CommonID
LEFT JOIN tblRight AS Right ON RightMax.MaxID = Right.[ID] AND Left.CommonID = Right.CommonID