How to show name of course in INNER JOIN? - php

I have two tables: users and courses. Inside users table i have filed course where i have course id. Inside courses table i have just ID and NAME.
I need to get popular course. I do request:
SELECT u.course, COUNT(*) as freq FROM users u INNER JOIN courses c ON u.course = c.id GROUP BY u.course
As a result: id => freq. But i need to replace ID to NAME of course. How?
Thanks.

You don't say what database you use, but I would assume you can use CTEs since most modern databases do. Your query can be written as:
with x as (
select course, count(*) as freq from users group by course
),
y as (
select max(freq) as max_freq from x
)
select c.name, x.freq
from x
join y on x.freq = y.max_freq
join courses c on c.id = x.course
This query has the [desirable?] side effect that it shows more than one course, if there are more than one tied in first place.

Add c.name to both the SELECT clause and the GROUP BY clause.
SELECT u.course, c.name, COUNT(*) as freq
FROM users u
INNER JOIN courses c
ON u.course = c.id
GROUP BY u.course, c.name;
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=02a41e0f1e6407e516e91c49b4bdc1d2

SELECT u.course, COUNT(*) as freq, c.name FROM users u INNER JOIN courses c ON u.course = c.id GROUP BY u.course

If your DBMS supports row_number this will be suitable:
select t.id, c.name, t.cnt
from course c
join (
select c.id, count(1) cnt, ROW_NUMBER() over(order by count(1) desc) rn
from users u
join course c on c.id = u.course
group by id
)t on t.id = c.id and t.rn = 1

Related

How to sum of two different table with two different groupby

I have two tables called work and stuff both tables have same fields -
company, quality, quantity
I need the sum of all the quantity of work with group by company and quality and join the table with sum of all the quantity of stuff with group by company and quality
I didn't get the expected result.
SQLfiddel
http://sqlfiddle.com/#!9/eea577/6
working query
select st.company,st.quality,st.quantitys - ct.quantitys as balance from
(select company,quality,sum(quantity) as quantitys from stuff
group by quality,company) as st join (select company,quality,
sum(quantity) as quantitys from work group by quality,company)
as ct on `ct`.`company` = `st`.`company` and ct.quality = st.quality group by quality,company
u have missed the quality condition in the join
Try the following:
SELECT tableA.ID, tableA.`Year`, tableA.`Month`,
tableA.`Type`, tableA.instrument,
tableA.totalAmount, tableB.totalInstrument
FROM
(
SELECT a.ID, a.`Year`, a.`Month`,
b.`Type`, b.instrument,
SUM(b.`amount`) totalAmount
FROM `date` a
INNER JOIN `transactions` b
ON a.ID = b.id
GROUP BY b.`Type
) tableA
INNER JOIN
(
SELECT a.ID, a.`Year`, a.`Month`,
b.`Type`, b.instrument,
SUM(b.`instrument`) totalInstrument
FROM `date` a
INNER JOIN `transactions` b
ON a.ID = b.id
GROUP BY a.`Year`, a.`Month`
) tableB ON tableA.ID = tableB.ID AND
tableA.`Year` = tableB.`Year` AND
tableA.`Month` = tableB.`Month`

select count of rows from 2 tables and merge into one row (mysqli)

i create a web app like facebook by php and mysqli
in my app i have a table for posts , one table for likes , and one table for comments
i want to get the number of comments and likes of each post in one row with his post_id!!!
i try some querys likes this :
select `tblpost`.`post_id`, COALESCE(TCOMM.`comment_num`,0) as `c_num`, COALESCE(TLIKE.`like_num`,0) as `l_num`
from
(select `tblpost`.`post_id`, count(*) as `like_num` from `tblpost` join `tbllikes` on `tbllikes`.`post_id` = `tblpost`.`post_id` group by `tblpost`.`post_id`
) TLIKE
inner join
(select `tblpost`.`post_id`, count(*) as `comment_num` from `tblpost` join `tblcomments` on `tblcomments`.`post_id` = `tblpost`.`post_id` group by `tblpost`.`post_id`) TCOMM
on
TCOMM.`post_id` = TLIKE.`post_id`
but i don't know what's my problem
You can do count distincts with two left joins.
Something like this would work if there are fields like_id and comment_id in the tables tbllikes and tblcomments
SELECT
tblpost.post_id AS post_id,
COUNT(DISTINCT tbllikes.like_id) AS likes,
COUNT(DiSTINCT tblcomments.comment_id) AS comments
FROM tblpost
LEFT JOIN tbllikes ON tbllikes.post_id = tblpost.post_id
LEFT JOIN tblcomments on tblcomments.post_id = tblpost.post_id
GROUP BY tblpost.post_id
First, I think you can greatly simplify your query:
select l.post_id,
COALESCE(c.comment_num, 0) as c_num, COALESCE(l.like_num, 0) as l_num
from (select l.post_id, count(*) as like_num
from tbllikes l
group by l.post_id
) l inner join
(select c.post_id, count(*) as comment_num
from tblcomments c
group by c.post_id
) c
on l.post_id = c.post_id;
This will only get you posts that have both likes and comments. To get what you want, use a left join:
select p.post_id,
COALESCE(c.comment_num, 0) as c_num, COALESCE(l.like_num, 0) as l_num
from tblpost p left join
(select l.post_id, count(*) as like_num
from tbllikes l
group by l.post_id
) l
on l.post_id = p.post_id left join
(select c.post_id, count(*) as comment_num
from tblcomments c
group by c.post_id
) c
on c.post_id = p.post_id;

MySQL query to order results by number of votes in another table for the specifc result/row

Okay, so I have a query below I am trying to get to work.
Basically everything works up until the inner join of the 'votes' table. What I am trying to do is order the results of this query in accordance with the number of votes each content row has in another table called votes. I know I'm not too far off from what I need to do!
Thanks in advance!!
mysql_query("
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
INNER JOIN votes ON votes.id = content.id
WHERE (content.type = 'pic')
ORDER BY COUNT(votes.id) DESC");
Try doing:
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
INNER JOIN (
SELECT id,COUNT(*) as voteCount
FROM votes
GROUP BY id
) v ON v.id = content.id
WHERE (content.type = 'pic')
ORDER BY v.voteCount DESC
When you do an INNER JOIN with votes table directly, if you have multiple occurrences of the same id, you will get a lot more rows than before you did the JOIN.
If you are only interested in the number of votes for each id, by doing a JOIN with a subquery that calculates the count of votes for each id, will leave your previous query results as they were, and lets you use the voteCount to order by it.
You could try this:
SELECT c.id, c.type, c.title, c.url, u.username, COUNT(v.id) votes
FROM content c
INNER JOIN users u ON c.uploaderuid = u.id
INNER JOIN votes v ON v.id = c.id
WHERE c.type = 'pic'
GROUP BY c.id, c.type, c.title, c.url, u.username
ORDER BY votes DESC
Here is the SQL Fiddle that demonstrates the below query:
SELECT c.id, c.type, c.title, c.url, u.username, COUNT(v.id)
FROM content AS c
INNER JOIN users AS u ON c.uploaderuid = u.id
INNER JOIN votes AS v ON v.id = c.id
WHERE c.type = 'pic'
GROUP BY c.id, c.type, c.title, c.url, u.username
ORDER BY COUNT(v.id) DESC

Issue with group by and order by clause with inner join

I have two tables namely locations and pilots. I have been trying to fetch data based on location_id i.e selecting pilots who are flying in a particular location_id order by date(date of schedule).
I am using group by as i need only distinct pilots to be displayed.
select B.*,
A.rather_to_be_flying_now,
A.here_now,
A.flying,
A.when,
A.my_favorite,
A.start,
A.end,
A.Locationid
from locations A
inner join pilots B
on A.Pilotid=B.pilot_id
where A.VenueID='$venueid'
and (A.flying='1' or A.here_now='1')
group by A.Pilotid
ORDER BY A.start
The query works good if i wont include a group by clause. It returns the following result
with out group by clause
with group by clause
But the above table shows wrong order, as the output must return start time as 2013-01-24 02:00:00 for pilotid 1 (Chronological order).
You can use MIN()
select B.*,
A.rather_to_be_flying_now,
A.here_now,
A.flying,
A.when,
A.my_favorite,
MIN(A.start) as start,
A.end,
A.Locationid
from locations A
inner join pilots B
on A.Pilotid=B.pilot_id
where A.VenueID='$venueid'
and (A.flying='1' or A.here_now='1')
group by A.Pilotid
ORDER BY A.start
Try this instead:
SELECT
B.*,
A.rather_to_be_flying_now,
A.here_now,
A.flying,
A.when,
A.my_favorite,
A.start,
A.end,
A.Locationid
FROM locations A
INNER JOIN
(
SELECT pilotid, MIN(start) MinStart
FROM locations
GROUP BY pilotid
) a2 ON A.pilotId = a2.pilotId
AND a.start = a2.minStart
INNER JOIN pilots B on A.Pilotid = B.pilot_id
WHERE A.VenueID = '$venueid'
AND (A.flying='1' OR A.here_now='1');
ORDER BY A.start ;
This will give you only those pilots with the minimum start date.
Try this query -
SELECT
p.pilit_id, l.location_id, l.start
FROM pilots p
JOIN (SELECT l1.*
FROM locations l1
JOIN (SELECT location_id, MIN(start) start
FROM locations
GROUP BY locations) l2
ON l1.id = l2.id AND l1.start = l2.start
) l
ON l.pilot_id = p.pilot_id
GROUP BY p.pilot_id
Add your WHERE condition.

MySQL joining tables with ORDER BY gives unexpected result

I have this database design:
**users_tbl**
id
username
name
**posts_tbl**
id
url
users_id *FK REFERENCE to users table*
**posts_contents_tbl**
id
posts_id *FK REFERENCE to posts table
title
description
date
views
click
isDeleted
I'm using this query
SELECT a.name,a.username,c.*
FROM users_tbl a
LEFT JOIN posts_tbl b ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC
Why I try to run this query it gives me NULL results, sample output is like this
But when I try to remove the ORDER BY c.id ASC it gives me this output:
That's not my expected result.
My expected result would be it will display the posts_contents_tbl in Ascending order at the same time it won't show some null values. Some users in my database doesn't have posts data in the posts_tbl so they should not show too.
How would I do that one? Your help would be greatly appreciated and rewarded!
Thanks!
PS: I already have thousands record in my database.
In that case, you have to use INNER JOIN instead of LEFT JOIN because you only want users with posts to show. The reason why there are Null values is because the records are based on table users_tbl and you've mentioned that some of them have no post. Right?
Try this:
SELECT a.name,
a.username,
c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
INNER JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY c.`date` DESC
I think this is what you are looking for:
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY IFNULL(c.id, 0) ASC;
If you really needs that posts_tbl data should not display if not available.And all data of posts_contents_tbl then you need a RIGHT JOIN and INNER JOIN .
The Query like :-
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b ON a.id = b.users_id
RIGHT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC;

Categories