I have two table called tbl_calendar and tbl_affiliates_link_history, tbl_calendar I am using for fill missing dates in result.
If I use query like below its work fine
SELECT dates as date,COUNT(a.id)AS total_visits FROM tbl_calendar as c LEFT JOIN tbl_affiliates_link_history as a ON c.dates = DATE(a.link_visit_time) WHERE c.dates > '2022-02-01' AND c.dates < '2022-02-13' GROUP BY c.dates
and giving me result like below
But if I add one additional where condition in my query called a.affiliate_id='wilson'
It's giving me only one result instead of full result like first image
My second query is like this
SELECT dates as date,COUNT(a.id)AS total_visits FROM tbl_calendar as c LEFT JOIN tbl_affiliates_link_history as a ON c.dates = DATE(a.link_visit_time) WHERE c.dates > '2022-02-01' AND c.dates < '2022-02-13'AND a.affiliate_id='wilson' GROUP BY c.dates
According to the LEFT JOIN properties, if the record doesn't join with another record into the joining table, all fields of the logical table created with the join are NULL.
This means if you search anything on the joined table on NULL field the search automatically excludes the NULL fields and at this point you are using the LEFT JOIN as an INNER JOIN.
To solve this, you need to add OR field IS NULL to the WHERE condition to keep the NULL results. The second query should be:
SELECT dates as date, COUNT(a.id) AS total_visits
FROM tbl_calendar as c
LEFT JOIN tbl_affiliates_link_history as a ON c.dates = DATE(a.link_visit_time)
WHERE c.dates > '2022-02-01' AND c.dates < '2022-02-13' AND
(a.affiliate_id='wilson' OR a.affiliate_id IS NULL)
GROUP BY c.dates
Related
I am trying to SELECT data from my database with the method LEFT OUTER JOIN but when I run my syntax the result is just one row. I am sure the result must be more rows.
I also tried the method FULL OUTER JOIN instead of LEFT OUTER JOIN. But when I do that I get an syntax error.
Does someone know why I am gettin just one row?
Here is my sql syntax:
SELECT
cus.cus_id
, cus.name_cus
, cus.address, count(invoice.id) as id2
, CONCAT('€ ', ROUND(SUM(invoice.total),2)) as total
, cus.id
FROM cus
LEFT OUTER JOIN invoice
ON cus.cus_id = invoice.cus_id
You are using aggregate functions without GROUP BY clause:
SELECT
cus.cus_id
, cus.name_cus
, cus.address
, count(invoice.id) as id2
, CONCAT('€ ', ROUND(SUM(invoice.total),2)) as total
, cus.id
FROM cus
LEFT OUTER JOIN invoice ON cus.cus_id = invoice.cus_id
GROUP BY cus.cus_id, cus.name_cus, cus.address, cus.id
Although you need to group only by the unique ID (i.e. cus.id) you should add other fields that are not aggregated to GROUP BY clause as well, even though they do not create additional groups.
I'm trying to see if I have some unused SIP numbers, so I have a list of numbers in the table tmp_numbers
0203XXXXX00
0203XXXXX01
0203XXXXX02
0203XXXXX03
...
The query I use is:
SELECT
n.number,
COUNT(c.did) AS count
FROM tmp_numbers n
LEFT JOIN cdr c
ON n.number = c.did
WHERE c.calldate >= '2017-01-01'
GROUP BY n.number
ORDER BY n.number
I get results from the above query, but it omits numbers it can't match on c.did.
I was under the impression that a LEFT JOIN would match/display everything on left table (tmp_numbers), regardless if there is a value on the right side (so it will show NULL?)
What am I missing here?
Put the where condition in the left join. Otherwise it turns implicitly into an inner join because conditions in the where clause filter on all data and if calldate is NULL then the condition is FALSE
LEFT JOIN cdr c ON n.number = c.did
AND c.calldate >= '2017-01-01'
You are querying on a column from the JOINED table which maybe null if there is no corresponding entry, so change the WHERE condition from:
WHERE c.calldate >= '2017-01-01'
to
WHERE c.calldate >= '2017-01-01' OR c.did IS NULL
I want to left join two table access_event_logs and hrm_personal_information its works perfectly.
Query is:
select concat(first_name," ",middle_name," ",last_name) as Name,
h.TIMESTAMPS,
a.PBI_ID
from
access_event_logs h LEFT JOIN hrm_personal_information a on h.USERID=a.PBI_ID
WHERE 1 group by DATE(h.TIMESTAMPS);
But i use where condition with between operator left join could not work .
Query is:
select concat(first_name," ",middle_name," ",last_name) as Name,
h.TIMESTAMPS,
a.PBI_ID
from
access_event_logs h LEFT JOIN hrm_personal_information a on h.USERID=a.PBI_ID
WHERE h.USERID=100032 and
h.TIMESTAMPS between '2015-11-02' and '2016-11-20'
group by DATE(h.TIMESTAMPS);
How can I solve it? please help me.
as its timestamp you either need to convert timestamp field to date or add time in your comparison
like
date(h.TIMESTAMPS) between '2015-11-02' and '2016-11-20'
or
h.TIMESTAMPS between '2015-11-02 00:00:01' and '2016-11-20 23:59:59'
I am wanting to display results where the date stored in the table is not between the dates specified in the query.
Here is the current SQL query
SELECT accounts_cstm.statusdescription_c,
users.user_name,
accounts.name,
accounts_cstm.account_number_c,
DATE_FORMAT(MAX(calls.date_modified),'%Y/%m/%d')
FROM accounts
LEFT OUTER JOIN calls ON accounts.id = calls.parent_id
LEFT OUTER JOIN users ON accounts.assigned_user_id = users.id
LEFT OUTER JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c
AND accounts.deleted <> 1
WHERE
(SELECT DATE_FORMAT(MAX(calls.date_modified),'%Y/%m/%d')
FROM calls) NOT BETWEEN '2014/06/25' AND '2014/07/02'
AND users.user_name = 'CBennet'
AND accounts_cstm.chkcustomer_c = '1'
GROUP BY accounts.name
I get a full list of results but I get results that shouldn't appear ie results with calls.date_modified that is in between the dates specified.
See below for an example of a wrong result, you can see that the date to the right is in between the dates 2014/06/25 and 2014/07/02 therefore this shouldn't appear.
Can someone let me know what i'm doing wrong here?
Within the table calls, date_modified is stored in the following format 2014-06-10 10:55:47
try this
SELECT *
FROM `test`
WHERE (date NOT BETWEEN '2012-01-30 14:15:55' AND '2014-09-29 10:15:55')
I created test table with some test values at sqlfiddle and got desired output http://sqlfiddle.com/#!2/5ffb7/4
I have a problem with the ROLLUP, I have rows with null values, and the ROLLUP also returns null, how do I difference between the null values of the ROLLUP and the null values of the row?
The null in the rows exist because the column (group_name) is associated with a left join.
Here is my query:
SELECT gr.info,
HOUR(cdr.calldate) AS hour,
DATE(cdr.calldate) AS date,
COUNT(DISTINCT cdr.uniqueid) AS count,
pl.number,
IFNULL(ugr.group_name, "") AS group_name
FROM cdr
INNER JOIN callinfo AS ci
ON ci.uniqueid = cdr.uniqueid
LEFT JOIN users AS usr
ON usr.username = ci.agent
LEFT JOIN groups AS ugr
ON ugr.group_id = usr.group_id
INNER JOIN pstnline AS pl
ON ci.line = pl.number
INNER JOIN hunt_line AS gri
ON gri.pstnline_id = pl.pstnline_id
INNER JOIN hunt AS gr
ON gri.hunt_number = gr.number
WHERE cdr.calldate >='2012-12-01 00:00'
AND cdr.calldate <='2013-01-24 10:45'
GROUP BY group_name WITH ROLLUP
I see that in SQL Server exist a function called GROUPING, but in MySql doesn't exist, how can i achieve the same effect?
I think you can also do this in the query that you have, by changing the group by argument to:
group by ifnull(ugr.group_name, '')
Now, blanks will indicate NULLs from the outer join and NULLs will indicate rollup.