Exclude a certain row - mysql

I have a query with several joins.
My query returns the top ten people for a leader-board based on their total steps.
I want to exclude all rows that have a value in the privacy column set to out.
MySQL query as it stands:
SELECT ga.owner_id, u.displayname, g.title, SUM(ga.steps) as `Total Steps`, u.user_id, g.group_id, pri.privacy FROM engine4_passport_goalactivitys ga
LEFT JOIN engine4_passport_goals goals ON goals.goal_id = ga.owner_id
LEFT JOIN engine4_passport_passports p ON p.passport_id = goals.owner_id
LEFT JOIN engine4_users u ON u.user_id = p.owner_id
LEFT JOIN engine4_passport_teams t ON t.owner_id = u.user_id
LEFT JOIN engine4_group_groups g ON g.group_id = t.group_id
LEFT JOIN engine4_passport_privacy pri on pri.user_id = u.user_id
GROUP BY u.user_id
ORDER BY `Total Steps` DESC
LIMIT 0,10
I have tried:
SELECT ga.owner_id, u.displayname, g.title, SUM(ga.steps) as `Total Steps`, u.user_id, g.group_id, pri.privacy FROM engine4_passport_goalactivitys ga
LEFT JOIN engine4_passport_goals goals ON goals.goal_id = ga.owner_id
LEFT JOIN engine4_passport_passports p ON p.passport_id = goals.owner_id
LEFT JOIN engine4_users u ON u.user_id = p.owner_id
LEFT JOIN engine4_passport_teams t ON t.owner_id = u.user_id
LEFT JOIN engine4_group_groups g ON g.group_id = t.group_id
LEFT JOIN engine4_passport_privacy pri on pri.user_id = u.user_id
AND pri.privacy <> 'out'
GROUP BY u.user_id
ORDER BY `Total Steps` DESC
LIMIT 0,10
The difference between the two queries above is the line:
AND pri.privacy <> 'out'
however, instead of excluding users with privacy set to 'out' it includes them in the query and just sets their privacy value to null
I have also tried where instead of and but it just returns 0 rows.

Try using it in WHERE clause instead of AND
where pri.privacy <> 'out'

SOLVED -
SELECT ga.owner_id, u.displayname, g.title, SUM(ga.steps) as `Total Steps`, u.user_id, g.group_id, pri.privacy FROM engine4_passport_goalactivitys ga
LEFT JOIN engine4_passport_goals goals ON goals.goal_id = ga.owner_id
LEFT JOIN engine4_passport_passports p ON p.passport_id = goals.owner_id
LEFT JOIN engine4_users u ON u.user_id = p.owner_id
LEFT JOIN engine4_passport_teams t ON t.owner_id = u.user_id
LEFT JOIN engine4_group_groups g ON g.group_id = t.group_id
LEFT JOIN engine4_passport_privacy pri on pri.user_id = u.user_id
WHERE pri.privacy is null OR pri.privacy = 'in'
GROUP BY u.user_id
ORDER BY `Total Steps` DESC
LIMIT 0,10;

Related

mysql LEFT join query optimize

i have query
SELECT P.*, COUNT(L.to) AS likes, U.name AS ownerName, U.username AS ownerUsername,
U.picture AS ownerPicture
FROM sn_posts P LEFT JOIN sn_users AS U
ON U.id = P.ownerID
LEFT JOIN sn_likes AS L
ON L.to = P.id
WHERE (P.ownerID = 69)
GROUP BY P.id
ORDER BY P.id DESC
it's taking - 0.3337 sec time,
Add an index for table sn_posts with ownerID field may
be nicer

mysql join with multiple tables and count query

I have total 6 tables in which different info has been saved
Now i need a result in which get count from 5 tables and select all info from main table but if record does not exist than it must be need to return 0 instead of no row found that's the problem here
I have tried below query but didn't get success
SELECT
u.*,
COUNT(DISTINCT c.id) as comments,
COUNT(DISTINCT d.id) as dislikes,
COUNT(DISTINCT l.id) as likes,
COUNT(DISTINCT s.id) as shares,
COUNT(DISTINCT t.id) as tags
FROM
job_details as u
JOIN job_comments as c ON u.id = c.job_id
JOIN job_dislike as d ON u.id = d.job_id
JOIN job_like as l ON u.id = l.job_id
JOIN job_share as s ON u.id = s.job_id
JOIN job_tags as t ON u.id = t.job_id
WHERE
u.id = c.job_id AND
u.id = d.job_id AND
u.id = l.job_id AND
u.id = s.job_id AND
u.id = t.job_id
GROUP BY
u.id
This query is executed, but didn't get exact result.
I don't quite understand why.
I was hoping somebody here could help me out?
Thanks!
You probably didn't get the exact result because some tables may be missing values.
Although you can solve this problem with a LEFT JOIN, the safer solution is to pre-aggregate the data:
SELECT u.*, c.comments, d.dislikes, l.likes, s.shares, t.tags
FROM job_details as u LEFT JOIN
(select c.job_id, count(*) as comments from job_comments group by c.job_id
) c
ON u.id = c.job_id LEFT JOIN
(select d.job_id, count(*) as dislikes from job_dislike d group by d.job_id
) d
ON u.id = d.job_id LEFT JOIN
(select l.job_id, count(*) as likes from job_like l group by l.job_id
) l
ON u.id = l.job_id LEFT JOIN
(select s.job_id, count(*) as shares from job_share s group by s.job_id
) s
ON u.id = s.job_id LEFT JOIN
(select t.job_id, count(*) as tags from job_tags t group by t.job_id
) t
ON u.id = t.job_id;
Why is this better? Consider an id that has 5 comments, likes, dislikes, shares and tags. The JOIN approach produces an intermediate result with 5*5*5*5*5 = 3,125 intermediate rows. Things can really get out of hand for popular ids.
Use LEFT JOIN instead of JOIN. and you don't need WHERE clause since you have joined those tables. And, use IFNULL function to return 0 for null values. You need to modify you query like this :
SELECT u.id,
IFNULL(COUNT(DISTINCT c.id),0) as comments,
IFNULL(COUNT(DISTINCT d.id),0) as dislikes,
IFNULL(COUNT(DISTINCT l.id),0) as likes,
IFNULL(COUNT(DISTINCT s.id),0) as shares,
IFNULL(COUNT(DISTINCT t.id),0) as tags
FROM job_details as u
LEFT JOIN job_comments as c ON u.id = c.job_id
LEFT JOIN job_dislike as d ON u.id = d.job_id
LEFT JOIN job_like as l ON u.id = l.job_id
LEFT JOIN job_share as s ON u.id = s.job_id
LEFT JOIN job_tags as t ON u.id = t.job_id
GROUP BY u.id

Add additional joins to already joined table in SQL

I have a existing query where I am using joins (thanks RADAR) to get my data.
Working SQL
SELECT
IFNULL(f.field_full_name_value, 'No Value'), u.name, u.uid, n.title, n.nid, a.timestamp, d.field_video_duration_value AS duration
FROM
db_node_view_count a
join db_node n
ON a.nid = n.nid
JOIN db_field_data_field_video_duration d
ON n.nid = d.entity_id
JOIN db_users u
ON a.uid = u.uid
AND u.uid <> 1
LEFT JOIN db_field_data_field_full_name f
ON u.uid = f.entity_id
ORDER BY u.uid desc
What I want to do is that I want to extend the query to show roles of a db_users u. There is a table called db_roles, which contain all role names with a primary key rid. Then the second table is db_users_roles, which contains a matching uid (from db_users u) and rid to show which user selected which role.
So what I did was that under ON a.uid = u.uid, I added JOIN db_users_roles ur ON u.uid = ur.uid JOIN db_role r ON ur.rid = r.rid. It works fine but it shows duplicate rows. Any idea why it's happening?
SELECT
IFNULL(f.field_full_name_value, 'No Value'), r.name, u.name, u.uid, n.title, n.nid, a.timestamp, d.field_video_duration_value AS duration
FROM
db_node_view_count a
join db_node n
ON a.nid = n.nid
JOIN db_field_data_field_video_duration d
ON n.nid = d.entity_id
JOIN db_users u
ON a.uid = u.uid
LEFT JOIN db_users_roles ur
ON u.uid = ur.uid
LEFT JOIN db_role r
ON ur.rid = r.rid
AND u.uid <> 1
LEFT JOIN db_field_data_field_full_name f
ON u.uid = f.entity_id
ORDER BY u.uid desc
UPDATE
With help from Joe, here is a slight update:
SELECT
IFNULL(f.field_full_name_value, 'No Value'), GROUP_CONCAT(r.name), u.name, u.uid, n.title, n.nid, a.timestamp, d.field_video_duration_value AS duration
FROM
db_node_view_count a
join db_node n
ON a.nid = n.nid
JOIN db_field_data_field_video_duration d
ON n.nid = d.entity_id
JOIN db_users u
ON a.uid = u.uid
LEFT JOIN db_users_roles ur
ON u.uid = ur.uid
LEFT JOIN db_role r
ON ur.rid = r.rid
AND u.uid <> 1
LEFT JOIN db_field_data_field_full_name f
ON u.uid = f.entity_id
GROUP BY f.field_full_name_value
ORDER BY u.uid desc
Some users in your (Drupal) database may have more than one role. So where your original query had (simplified a bit) one row per node, the modified query will duplicate the rows for each role of the user.
You might want to modify the query to include
GROUP BY n.nid, u.uid
and change the SELECT field list to include GROUP_CONCAT(r.name) rather than r.name.

Pass Value to Subselect

SELECT
u.*,
GROUP_CONCAT(DISTINCT f.shot_id SEPARATOR ",") AS ownFavorites,
GROUP_CONCAT(DISTINCT st.shot_id SEPARATOR ",") AS ownStars,
GROUP_CONCAT(DISTINCT s.id SEPARATOR ",") AS ownShots,
( SELECT AVG(p.count)
FROM points p
LEFT JOIN shots s ON s.user_id = **U.ID** AND p.shot_id = s.id
WHERE date >= DATE_SUB(CURDATE(),INTERVAL 2 DAY)
) AS attention,
( SELECT SUM(p.count)
FROM points p
LEFT JOIN shots s ON s.user_id = **U.ID** AND s.id = p.shot_id
) AS popularity
FROM users u
LEFT OUTER JOIN shots s ON s.user_id = u.id
LEFT OUTER JOIN favorites f ON f.user_id = u.id
LEFT OUTER JOIN stars st ON st.user_id = u.id
WHERE u.username = ?;
I got two subselects which use the parameter u.id (marked in the query). If i do the sql like this it will generate somthing like that:
#1054 - Unknown column 'u.id' in 'on clause'
Means, the u.id id is NOT defined in the SubSelects. But in the MainSelect I choose from the users table, where u.id exists.
To my question: Is there a way to pass the selected u.id value to the Subselects with common sql?
Don't forget GROUP BY in the subqueries:
SELECT
u.*,
COALECSE(a.average, 0) attention,
COALESCE(p.total, 0) popular,
GROUP_CONCAT(DISTINCT f.shot_id) AS ownFavorites,
GROUP_CONCAT(DISTINCT st.shot_id SEPARATOR ",") AS ownStars,
GROUP_CONCAT(DISTINCT s.id SEPARATOR ",") AS ownShots
FROM
users u
LEFT JOIN
(
SELECT
s.user_id,
AVG(p.count) average
FROM
shots s
JOIN
points p
ON s.id = p.shot_id
WHERE
s.date >+ CURRENT_DATE - INTERVAL 2 DAY
GROUP BY s.user_id
) a
ON u.id = a.user_id
LEFT JOIN
(
SELECT
s.user_id,
SUM(p.count) total
FROM
shots s
JOIN
points p
ON s.id = p.shot_id
GROUP BY s.user_id
) p
ON u.id = p.user_id
LEFT OUTER JOIN shots s ON s.user_id = u.id
LEFT OUTER JOIN favorites f ON f.user_id = u.id
LEFT OUTER JOIN stars st ON st.user_id = u.id
WHERE u.username = 'user'
Seems like this may work. The select doesn't have knowledge of the of the users table the way you had it. I believe this would have knowledge of Users.
SELECT
u.*,
GROUP_CONCAT(DISTINCT f.shot_id SEPARATOR ",") AS ownFavorites,
GROUP_CONCAT(DISTINCT st.shot_id SEPARATOR ",") AS ownStars,
GROUP_CONCAT(DISTINCT s.id SEPARATOR ",") AS ownShots,
A.Attention, P.Popularity
FROM users u
LEFT OUTER JOIN shots s ON s.user_id = u.id
LEFT OUTER JOIN favorites f ON f.user_id = u.id
LEFT OUTER JOIN stars st ON st.user_id = u.id
LEFT OUTER JOIN
( SELECT AVG(p.count) attention
FROM points p
LEFT JOIN shots s ON s.user_id = **U.ID** AND p.shot_id = s.id
WHERE date >= DATE_SUB(CURDATE(),INTERVAL 2 DAY)
) AS A,
( SELECT SUM(p.count) popularity
FROM points p
LEFT JOIN shots s ON s.user_id = **U.ID** AND s.id = p.shot_id
) AS P
WHERE u.username = ?;
Try turning the selects into a subselect join.
FROM users u
LEFT OUTER JOIN shots s ON s.user_id = u.id
LEFT OUTER JOIN favorites f ON f.user_id = u.id
LEFT OUTER JOIN stars st ON st.user_id = u.id
LEFT OUTER JOIN ( SELECT AVG(p.count) AverageOfP, p.shot_id
FROM points p
WHERE date >= DATE_SUB(CURDATE(),INTERVAL 2 DAY)
) p ON p.shot_id = s.id
LEFT OUTER JOIN ( SELECT SUM(p.count) SumOfP, p.shot_id
FROM points p
) p2 ON p2.shot_id = s.id
The s table is already joined to u and should be good. Then in your select you can just select AverageOfP and SumOfP.

Problems with reusing LEFT JOIN results in WHERE and ORDER BY Clause

SELECT s.*,
u.username,
u.fullname,
c.title AS ctitle,
c.description AS cdescription,
sa.attention,
sp.popularity,
COUNT(DISTINCT f.id) AS favorites,
COUNT(DISTINCT st.id) AS stars,
COUNT(DISTINCT v.id) AS views
FROM shots s
INNER JOIN users u ON u.id = s.user_id
INNER JOIN categories c ON c.id = s.cat_id
LEFT OUTER JOIN(
SELECT shot_id, round(AVG(count),2) AS attention
FROM points
WHERE date > DATE_SUB(CURDATE(),INTERVAL 2 DAY)
GROUP BY shot_id
) sa ON sa.shot_id = s.id
LEFT OUTER JOIN(
SELECT shot_id, SUM(count) AS popularity
FROM points
GROUP BY shot_id
) sp ON sp.shot_id = s.id
LEFT OUTER JOIN favorites f ON f.shot_id = s.id
LEFT OUTER JOIN stars st ON st.shot_id = s.id
LEFT OUTER JOIN views v ON v.shot_id = s.id
**WHERE s.library = 1 AND sa.attention > 40
ORDER BY sa.attention DESC
LIMIT 0,50**
GROUP BY s.id
I can't use the sa.attention in a condition and for ordering. Why?
(I removed the marked part, and the query works!)
What do I have to change in my Query? And if you could give a explanation for it, that would be very nice!
You are negating your OUTER JOIN by putting that in your WHERE criteria. Move it to your JOIN and you'll get your NULL records back:
SELECT s.*,
u.username,
u.fullname,
c.title AS ctitle,
c.description AS cdescription,
sa.attention,
sp.popularity,
COUNT(DISTINCT f.id) AS favorites,
COUNT(DISTINCT st.id) AS stars,
COUNT(DISTINCT v.id) AS views
FROM shots s
INNER JOIN users u ON u.id = s.user_id
INNER JOIN categories c ON c.id = s.cat_id
LEFT OUTER JOIN(
SELECT shot_id, round(AVG(count),2) AS attention
FROM points
WHERE date > DATE_SUB(CURDATE(),INTERVAL 2 DAY)
GROUP BY shot_id
) sa ON sa.shot_id = s.id AND sa.attention > 40
LEFT OUTER JOIN(
SELECT shot_id, SUM(count) AS popularity
FROM points
GROUP BY shot_id
) sp ON sp.shot_id = s.id
LEFT OUTER JOIN favorites f ON f.shot_id = s.id
LEFT OUTER JOIN stars st ON st.shot_id = s.id
LEFT OUTER JOIN views v ON v.shot_id = s.id
WHERE s.library = 1
GROUP BY s.id
ORDER BY sa.attention DESC
LIMIT 0,50
A second note, GROUP BY cannot go at the end. I moved that to the correct spot as well.