retrive all data with counting per item when joining two table - mysql

I have two tables:
Table 1: contest_video
Table Rows: cv_id, uid, cid, cn_id, video_name, video_detail, video, image, cverify, date
Table 2: contest_vote
Table Rows: vid, cv_id, voter_id, date
Now I want to join these two table that will with a counting. Like total number of vote (vid) of a video(video name)
I have tried this way:
SELECT *
FROM (`contest_video`)
LEFT JOIN `system_user`
ON `system_user`.`id` = `contest_video`.`uid`
LEFT JOIN `contest_vote`
ON `contest_vote`.`cv_id` = `contest_video`.`cv_id`
WHERE `contest_video`.`cid` = '1'
ORDER BY `contest_video`.`created_date` DESC
But it returns only data without counting. Need experts help please.

You can do it like this
use group by and column names instead of *
SELECT
cv_id,
uid,
cid,
cn_id,
video_name,
video_detail,
video,
image,
cverify,
date,
count(`contest_video`.`cv_id`) Total
FROM (`contest_video`)
LEFT JOIN `system_user`
ON `system_user`.`id` = `contest_video`.`uid`
LEFT JOIN `contest_vote`
ON `contest_vote`.`cv_id` = `contest_video`.`cv_id`
WHERE `contest_video`.`cid` = '1'
GROUP BY `contest_video`.`video_name`
ORDER BY `contest_video`.`created_date` DESC

Related

Counting one field in sql with join

I have a table, named rendeles_termekek.(In english, ordered_products)
I would like to count, that each product how many times was ordered.
With the SQL below, I get 4 as ennyi. I upload a picture, and I wrote the correct number to each rows.
SELECT DISTINCT
rendeles_termekek.termek_id,
termek.termek_id,
termek.termek_nev,
( SELECT COUNT(rendeles_termekek.termek_id) FROM rendeles_termekek ) AS ennyi
FROM rendeles_termekek
LEFT JOIN termek ON rendeles_termekek.termek_id = termek.termek_id
ORDER BY termek.termek_nev ASC
**r1** is the alias of your table rendeles_termekek so you have to access those columns through alias name "r1" like I did in below query. try below
SELECT DISTINCT
r1.termek_id,
termek.termek_id,
termek.termek_nev,
( SELECT COUNT(r.termek_id) FROM rendeles_termekek r where r.termek_id = r1.termek_id ) AS ennyi
FROM rendeles_termekek r1
LEFT JOIN termek ON r1.termek_id = termek.termek_id
ORDER BY termek.termek_nev ASC
I think you have only four rows in your table "rendeles_termekek".
I have updated your query. try this
SELECT DISTINCT
rendeles_termekek.termek_id,
termek.termek_id,
termek.termek_nev,
( SELECT COUNT(rendeles_termekek.termek_id) FROM rendeles_termekek r where r.termek_id = r1.termek_id ) AS ennyi
FROM rendeles_termekek r1
LEFT JOIN termek ON rendeles_termekek.termek_id = termek.termek_id
ORDER BY termek.termek_nev ASC
Note that I have changed the sub-query and added a where condition in it
`( SELECT COUNT(rendeles_termekek.termek_id) FROM rendeles_termekek r where r.termek_id = r1.termek_id ) AS ennyi`

LEFT JOIN SUM with WHERE clause

The following query always outputs SUM for all rows instead of per userid. Not sure where else to look. Please help.
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
) assignments ON (userid = assignments.userid AND ticketid = ?)
WHERE ticketid = ?
ORDER BY assigned,scheduled
If you want to keep the SELECT *, you would have to add a group by clause in the subquery. Something like this
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
GROUP BY userid
) time_entriesSummed ON time_entriesSummed.userid = assignments.userid
WHERE ticketid = ?
ORDER BY assigned,scheduled
But a better way would be to change the SELECT * to instead select the fields you want a add a group by clause directly. Something like this
SELECT
assignments.id,
assignments.assigned,
assignments.scheduled,
SUM(time_entries.timeworked) AS totalTimeworked
FROM assignments
LEFT JOIN time_entries
ON time_entries.userid = assignments.userid
GROUP BY assignments.id, assignments.assigned, assignments.scheduled
Edit 1
Included table names in query 2 as mentioned in chameera's comment below

Join tables with specific order

I've got two tables, for example: Teacher and Pupil and table LastViewedPupil with fields who watched him and when (teacherId & pupilId). So I want to return the list of Pupils that was ordered by last viewed date, but there are not all pupils inside LastViewedPupil, but last few for example, I want to show after that ordered by date all left records no matter in wich order, how can I do that?
I can do without last part like
select * from Pupil as p, (
select * from LastViewedPupil lvp where lvp.teacherId = 5 ORDER BY lastViewDate
) as lvp where lvp.pupilId = p.pupilId;
Or should I add corresponding records in LastViewDatePupil for all pupils or need to Join table itself (sounds awkward)?
You should try this one:
SELECT p.*
LEFT JOIN LastViewedPupil lvp ON p.id = lvp
WHERE lvp.teacher_id = 5
ORDER BY lvp.lastViewDate DESC
I'm not sure if that query puts NULL at the beginning or at the end. If that doesn't order the results properly, try this other. I used a CASE for reordering data
SELECT p.*,
CASE WHEN lvp.lvp.lastViewDate IS NULL THEN 1 ELSE 0, END AS notNullfirst FROM Pupil p
LEFT JOIN LastViewedPupil lvp ON p.id = lvp
WHERE lvp.teacher_id = 5
ORDER BY notNullfirst, lvp.lastViewDate DESC

SQL join and get another column

I'm trying to join two tables so I can easily order it, as one contains the names of items and the other doesn't. The tables:
user_games: UGID, UID, APPID, playtime
games_steam: APPID, name
my SQL so far:
SELECT user_games.*
FROM user_games
JOIN games_steam ON user_games.APPID = games_steam.APPID
WHERE user_games.UID = '76561197996836099'
GROUP BY user_games.APPID
ORDER BY games_steam.name ASC
LIMIT 0, 30
Just no idea how to get the name column into this aswell.
Is this what you are looking for?
SELECT user_games.*, games_steam.name
FROM user_games
JOIN games_steam
ON user_games.APPID = games_steam.APPID
WHERE user_games.UID = '76561197996836099'
GROUP BY user_games.APPID
ORDER BY games_steam.name ASC
LIMIT 0, 30

MySQL views cannot have subquery in from, and don't maintain order. How can I create a view from this query?

I'm essentially trying to obtain a resultset with each employee's current title. I'd like to create a view from this for later use, but I find I'm being stumped, and likely missing a simple solution. Here's the query in question, and thanks in advance!
select * from
(SELECT
appointment.employee_id,
title.`name` as title_name
FROM
appointment
INNER JOIN appointment_title ON appointment.id = appointment_title.appointment_id
INNER JOIN title ON appointment_title.title_id = title.id
order by appointment_title.effective_date DESC) tmp group by employee_id
Updated:
SELECT
appointment.employee_id ,
( SELECT title.`name`
FROM appointment_title
INNER JOIN title
ON appointment_title.title_id = title.id
WHERE appointment.id = appointment_title.appointment_id
ORDER BY appointment_title.effective_date DESC
LIMIT 1
) AS title_name
FROM appointment
GROUP BY appointment.employee_id
Another option is to break up the query into two views. The first view will contain the derived table subquery, and the second will simply select from that one:
CREATE VIEW vwEmployee_Inner AS
SELECT
appointment.employee_id,
title.`name` as title_name
FROM
appointment
INNER JOIN appointment_title ON appointment.id = appointment_title.appointment_id
INNER JOIN title ON appointment_title.title_id = title.id
order by appointment_title.effective_date DESC
And then your original view becomes:
CREATE VIEW vwEmployee AS
SELECT * FROM vwEmployee_Inner GROUP BY employee_id