I have 2 tables: course and section as shown in sqlfiddle
I need to select the 'starttime' (or null) for each course's section.
Some courses don't have a 'section' entry.
The query:
select section.id, section.course, section.section, IFNULL(schedule.starttime, "null")
from section
join schedule on schedule.courseid = section.course
where course = 3 and section.section > 0
returns has many duplicate entries as there are in the joined table.
id course section IFNULL(schedule.starttime, "null")
7 3 1 1440021600
7 3 1 1440021620
7 3 1 1440021640
8 3 2 1440021600
8 3 2 1440021620
8 3 2 1440021640
9 3 3 1440021600
9 3 3 1440021620
9 3 3 1440021640
When I expect:
id course section IFNULL(schedule.starttime, "null")
7 3 1 1440021600
8 3 2 1440021620
9 3 3 1440021640
10 3 4 null
11 3 5 null
I tried different combinations of DISTINCT and GROUP BY without success.
Try:
SELECT section.id, section.course, section.section, IFNULL(schedule.starttime, "null")
FROM section
LEFT JOIN schedule ON schedule.courseid = section.course AND schedule.id = section.section
WHERE course = 3 AND section.section > 0
you can use left join then use the schedule.sectionid and section.id
select section.id, section.course, section.section, IFNULL(schedule.starttime, "")
from section
LEFT JOIN schedule ON schedule.sectionid = section.id
where course = 3 and section.section > 0;
SEE DEMO
Related
This question already has answers here:
Left Outer Join doesn't return all rows from my left table?
(3 answers)
Closed 8 months ago.
I have two tables Broadcastlists and Contacts(foreign key of broadcastlist). I want to show broadcastlist tables all records and count of broadcastlistid in contacts table.
My Query:-
SELECT b.id, count(c.broadcastlist_id)as Recepients,b.name
from Broadcastlists b
LEFT JOIN Contacts c ON b.id = c.broadcastlist_id
group by c.broadcastlist_id;
Broadcastlists:
Id
Name
1
Test 1
2
Test 2
3
Test 4
4
Test 5
Contacts:
Id
Name
Broadcastlist_id
1
Rahul
2
2
Mansi
1
3
Nisha
2
4
Nidhi
2
5
Prashant
1
I want Output like this
Id
Name
Recepients(count)
1
Test 1
2
2
Test 2
3
3
Test 3
0
4
Test 4
0
But, Output come like this, shows only one null record from left table I want all null data from left table
Id
Name
Recepients(count)
1
Test 1
2
2
Test 2
3
3
Test 3
0
You have grouped with a wrong column. Try this:
SELECT b.id, b.name, COUNT(c.broadcastlist_id) AS Recepients
FROM Broadcastlists b
LEFT JOIN Contacts c ON b.id = c.broadcastlist_id
GROUP BY b.id, b.name;
Output
id
name
Recepients
1
Test 1
2
2
Test 2
3
3
Test 4
0
4
Test 5
0
See this db<>fiddle.
Now this question got me so bad i am even not sure about the question headline.
I have 4 tables (topic t, user u, entry e, user_fav u_f), respectively
topic_id topic_name
1 abba
2 queen
3 rolling stones
user_id user_name
1 a
2 b
3 c
4 d
entry_id entry topic_fk(FK to t.topic_id) user_fk(FK to u.user_id)
1 ..... 1 1
2 ..... 1 2
3 ..... 2 2
4 ..... 2 3
5 ..... 3 4
user_fav_id user_name entry_fk(FK to e.entry_id)
1 a 1
2 a 2
3 a 3
4 c 4
5 d 5
and my question is how i can reach to t.topic_name using u_f.user_name?
An example to be more precise: if i want to use user a,
i need to get u_f.entry_fk (which are 1, 2 & 3)
then i need to get e.topic_fk(which are 1, 1 & 2)
and finally, i need to get t.topic_name (which are abba, abba & queen) (pay attention i need 2 abba's with different entry_ids with them)
You can use sql joins. Try this:
SELECT t.topic_name, e.entry_id
FROM `user` u
LEFT JOIN entry e ON u.user_id=e.user_id
INNER JOIN topic t ON e.topic_id=t.topic_id
WHERE u.user_name='a';
I have two tables:
LLOAN
LOANID SOURCEID LOAN_COMPANY ETC
1 1 3
2 1 3
3 1 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
COMPANY
CompanyID CountryID CompanyIDLLAS
1 1 1
2 1 2
3 1 3
4 2 1
5 3 1
6 4 1
And I want to join them. The SourceID refers to the CountryID and the LOAN_COMPANY refers to the CompanyID. Only country '1' has multiple companies, all the others just have one.
How can I join these two tables correctly? I've tried many different things, of which this came the closest:
SELECT Count(c.CompanyID) FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON c.CountryID = l.SourceID AND c.CompanyID = l.LOAN_COMPANY
But it leaves many rows blank. What is the correct way to join two tables with two conditions?
Try below Query:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As LL
LEFT JOIN dbo.Company As C
ON (C.CountryID = LL.SourceID)
AND (C.CompanyID = LL.LOAN_COMPANY)
You can group the condition using paranthesis like this:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON (c.CountryID = l.SourceID) AND (c.CompanyID = l.LOAN_COMPANY)
i have a problem concerning a select query in MYSQL
i have two different tables and i want to obtain a certain result
i used COUNT method which gave me only the results (>=1)
But in reality , i want to use all counts with zero included how to do it?
My query is:
SELECT
first.subscriber_id,
second.tag_id,
COUNT(*)
FROM
content_hits first
JOIN content_tag second ON first.content_id=second.content_id
GROUP BY
second.Tag_id,first.Subscriber_id<br>
First table:Content_hits
CONTENT_ID SUBSCRIBER_ID
30 1
10 10
34 4
32 2
40 3
28 3
30 6
31 8
12 3
Second table:Content_tag
CONTENT_ID TAG_ID
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 2
12 2
13 2
14 2
Result but incomplete For example:Subsrciber6 for tag_id=1 should have a count(*)=0
subscriber_id tag_id COUNT(*)
1 1 4
2 1 7
3 1 2
4 1 1
5 1 3
7 1 2
8 1 1
9 1 1
10 1 3
1 2 2
2 2 3
3 2 2
Now that you have further elaborated on what you actually want to achieve, it can be seen that the problem is much more complex. You actually want all combinations of subscriber_id and tag_id, and then count the number of actual entries in the joined table product. whew. So here goes the SQL:
SELECT combinations.tag_id,
combinations.subscriber_id,
-- correlated subquery to count the actual hits by tag/subscriber when joining
-- the two tables using content_id
(SELECT count(*)
FROM content_hits AS h
JOIN content_tag AS t ON h.content_id = t.content_id
WHERE h.subscriber_id = combinations.subscriber_id
AND t.tag_id = combinations.tag_id) as cnt
-- Create all combinations of tag/subscribers first, before counting anything
-- This will be necessary to have "zero-counts" for any combination of
-- tag/subscriber
FROM (
SELECT DISTINCT tag_id, subscriber_id
FROM content_tag
CROSS JOIN content_hits
) AS combinations
Not sure, but is this what you want?
SELECT first.subscriber_id, second.tag_id, COUNT(*) AS c
FROM content_hits first JOIN content_tag second ON first.content_id=second.content_id
GROUP BY second.Tag_id,first.Subscriber_id HAVING c = 0
I have the following 2 tables:
1) Companies
ID CompanyName Abbreviation Notes
1 CompanyA CA ...
2 CompanyB CB ...
3 CompanyC CC ...
2) PlannedDeployments
ID CompanyID TypeID DepDate NumDeployed
1 1 2 09/2010 5
2 1 2 10/2010 5
3 1 3 09/2010 3
4 1 3 10/2010 3
5 1 4 10/2010 4
6 2 2 12/2010 10
7 2 4 10/2010 1
8 3 2 11/2010 6
Note that TypeID is a number between 1 and 5 describing what type of person is being deployed. For the purposes of this query, I'm interested in Type2 employees for each company and then the sum of Types 3 & 4 for each date. What I eventually want to end up with is a crosstab that looks like the following:
Crosstab
Date/Company CompanyA CompanyB CompanyC SumOfTypes3and4
09/2010 5 3
10/2010 5 8
11/2010 6
12/2010 10
The problem is that final column - the sum of Type 3 and Type 4 employees. The current crosstab that I have includes everything except that sum column and looks like the following:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate
FROM PlannedDeployments LEFT JOIN Companies ON Companies.ID=PlannedDeployments.CompanyID
WHERE PlannedDeployments.TypeID=2 AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control") AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate
PIVOT Companies.CompanyName;
The second part of that WHERE clause is just limiting the data by some form controls. Anyway - I'm having a lot of trouble getting that final column. Anyone have any ideas?
Edit: Building on the solution provided by Remou below, here's what the final query ended up looking like:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate, q.SumOfNumDeployed
FROM (SELECT PlannedDeployments.DepDate, Sum(PlannedDeployments.NumDeployed) AS SumOfNumDeployed
FROM PlannedDeployments
WHERE (((PlannedDeployments.[TypeID]) In (3,4)))
GROUP BY PlannedDeployments.DepDate) AS q
RIGHT JOIN (PlannedDeployments
INNER JOIN Companies ON PlannedDeployments.CompanyID = Companies.ID)
ON q.DepDate = PlannedDeployments.DepDate
WHERE PlannedDeployments.TypeID=2
AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control")
AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate, q.SumOfNumDeployed
PIVOT Companies.CompanyName;
You can use a subquery:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate, Sum(q.SumOfNumDeployed) AS SumOfSumOfNumDeployed
FROM (SELECT PlannedDeployments.DepDate, Sum(PlannedDeployments.NumDeployed) AS SumOfNumDeployed
FROM PlannedDeployments
WHERE (((PlannedDeployments.[TypeID]) In (3,4)))
GROUP BY PlannedDeployments.DepDate) AS q
RIGHT JOIN (PlannedDeployments
INNER JOIN Companies ON PlannedDeployments.CompanyID = Companies.ID)
ON q.DepDate = PlannedDeployments.DepDate
WHERE PlannedDeployments.TypeID=2
AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control")
AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate
PIVOT Companies.CompanyName;