subquery as the from for mysql - mysql

What am I doing wrong here? Trying to use a subquery as my FROM to ensure all joined tables are joined on the correct set.
SELECT
active_users.username as username,
active_users.computer_name as computer_name,
alert.cnt as alerts
FROM
(SELECT
computer_name,
username
FROM computers
INNER JOIN users
on users.computer_id = computers.computer_id
WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
) AS active_users
LEFT JOIN
(SELECT
user_id,
count(*) as cnt
from logs
group by user_id
) AS alert
on alert.user_id = active_users.user_id

You need to select user_id in the first subquery:
SELECT active_users.username as username, active_users.computer_name as computer_name,
alert.cnt as alerts
FROM (SELECT user_id, computer_name, username
FROM computers INNER JOIN
users
on users.computer_id = computers.computer_id
WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
) active_users LEFT JOIN
(SELECT user_id, count(*) as cnt
from logs
group by user_id
) alert
on alert.user_id = active_users.user_id;

Related

sum of 2 different queries result

I have to do sum from 2 different tables and show it using MySQL:
total comments from table 1, total comments from table 2: What i have tried so far is,
SELECT u.name as name, u.username as username,
( SELECT SUM(total) FROM (SELECT (COUNT(nc.id)) as total FROM table1 as nc WHERE nc.user_id = u.id) UNION ALL (SELECT COUNT(pc.id) AS total FROM table2 pc WHERE pc.user_id = u.id) as finalTotal ) as total_comments
FROM user as u
GROUP BY u.id
It is giving me this error:
Every derived table must have its own alias
If I understand what you need, you have to modify the query like this :
select u.name,u.username,total as total_comments
from user as u
left join (
select id,sum(total) as total
from(
select nc.id,count(1) as total
from table1 as nc
group by nc.id
union all
select pc.id,count(1) as total
from table2 as pc
group by pc.id
) as t group by id
) comments on comments.id = u.id
Before UNION ALL put alias like you giving for all..
SELECT u.name as name, u.username as username,
( SELECT SUM(total) FROM (SELECT (COUNT(nc.id)) as total FROM table1 as nc WHERE nc.user_id = u.id) as vr UNION ALL (SELECT COUNT(pc.id) AS total FROM table2 pc WHERE pc.user_id = u.id) as finalTotal ) as total_comments
FROM user as u
GROUP BY u.id
SELECT u.name as name, u.username as username,
( SELECT SUM(total) FROM
(SELECT (COUNT(nc.id)) as total FROM table1 as nc WHERE nc.user_id = u.id
UNION ALL SELECT COUNT(pc.id) AS total FROM table2 pc WHERE pc.user_id = u.id) as finalTotal ) as total_comments
FROM user as u
GROUP BY u.id
Remove brackets before and after UNION ALL and check.

SQL - Select discint AND count from JOIN query

I have below query:
SELECT u.*
(SELECT sum(trs.amount)
FROM transactions trs
WHERE u.id = trs.user AND trs.type = 'Recycle' AND
trs.TIME >= UNIX_TIMESTAMP(CURDATE())
) as amt
FROM (SELECT DISTINCT user_by
FROM xeon_users_rented
) AS xur JOIN
users u
ON xur.user_by = u.username
LIMIT 50
Which selects some data from my database. The above query works fine. However, I would like to also select count(*) from xeon_users_rented where user_by = u.username This is what I have attempted:
SELECT u.*
(SELECT sum(trs.amount)
FROM transactions trs
WHERE u.id = trs.user AND trs.type = 'Recycle' AND
trs.TIME >= UNIX_TIMESTAMP(CURDATE())
) as amt,
(SELECT DISTINCT count(*)
FROM xeon_users_rented
WHERE xur.user_by = u.username
) AS ttl
FROM (SELECT DISTINCT user_by
FROM xeon_users_rented
) AS xur JOIN
users u
ON xur.user_by = u.username
LIMIT 50
However, that gives me the total number of rows in xeon_users_rented as ttl - not the total distinct rows where username = user_by
I think you can do what you want just by tinkering with your subquery a little. That is, change the select distinct to a group by:
SELECT u.*, xur.cnt,
(SELECT sum(trs.amount)
FROM transactions trs
WHERE u.id = trs.user AND trs.type = 'Recycle' AND
trs.TIME >= UNIX_TIMESTAMP(CURDATE())
) as amt
FROM (SELECT user_by, COUNT(*) as cnt
FROM xeon_users_rented
GROUP BY user_by
) xur JOIN
users u
ON xur.user_by = u.username
LIMIT 50;
Some notes:
SELECT DISTINCT is not really necessary, because you can do the same logic using GROUP BY. So, it is more important to understand GROUP BY.
You are using LIMIT with no ORDER BY. That means that you can get a different set of rows each time you run the query. Bad practice.

Merge two SELECTs containing JOIN

I have two straightforward SELECT queries left-joining the same table in a MySQL DB:
SELECT uID, externaluID, COUNT(editID) AS editCount
FROM users
LEFT JOIN (edits
INNER JOIN posts
ON postRefID = postID AND editAuthorID <> authorID AND isa = 0)
ON editAuthorID = uID
GROUP BY uID
ORDER BY uID;
SELECT uID, externaluID, COUNT(posts.postID) AS postCount, SUM(value)
FROM users
LEFT JOIN (posts
LEFT JOIN usrR ON posts.postID = usrR.postID)
ON authorID = uID
GROUP BY authorID
ORDER BY uID;
So far so good. Now I want to merge these queries. My approach was
SELECT uID, externaluID, COUNT(editID) AS editCount, COUNT(P2.postID) AS postCount, SUM(rateValue)
FROM users
LEFT JOIN (edits
INNER JOIN posts AS P1
ON postRefID = P1.postID AND editAuthorID <> P1.authorID AND isa = 0)
ON editAuthorID = uID
LEFT JOIN (posts AS P2
LEFT JOIN usrR ON P2.postID = usrR.postID)
ON P2.authorID = uID
GROUP BY P2.authorID, uID
ORDER BY uID;
but it returns wrong results. What am I doing wrong?
Try this one:
SELECT u1.uID, u1.externaluID, COUNT(e1.editID) AS editCount, u2.postCount, u2.sumValue
FROM users u1
LEFT JOIN (edits INNER JOIN posts
ON postRefID = postID AND editAuthorID <> authorID AND isa = 0) as e1
ON e1.editAuthorID = u1.uID
LEFT JOIN (
SELECT uID, externaluID, COUNT(posts.postID) AS postCount, SUM(value) as sumValue
FROM users
LEFT JOIN (posts
LEFT JOIN usrR ON posts.postID = usrR.postID)
ON authorID = uID
GROUP BY authorID
ORDER BY uID) as u2 on u1.uID = u2.uID
GROUP BY u1.uID,u2.postCount, u2.sumValue
ORDER BY u1.uID

Adding another subquery

I have this query as my main query, I use all the records from the members table, and selected columns from comments and chat_box.
SELECT members.*,
a.commenter_id,
b.user_id,
a.comcount,
b.chatcount
FROM members
LEFT JOIN (SELECT commenter_id,
Count(*) comCount
FROM comments
GROUP BY commenter_id) a
ON members.id = a.commenter_id
LEFT JOIN (SELECT user_id,
Count(*) chatCount
FROM chat_box
GROUP BY user_id) b
ON members.id = b.user_id
WHERE members.id = '290'
I would like to add this query to the above
SELECT Count(friend_id) AS totFriend,
friend_id AS fi,
logged_user_id AS user,
friend_accepted AS fa
FROM member_friends
WHERE logged_user_id = '1'
AND friend_id = '290'
Is it possible to add this to the mix without causing any errors? I have tried it myself but I maybe just putting my self in deeper problems. If it is possible could someone assist me in doing so, thank you :).
If you have a way to join the last query to your members table, then you should be able to use:
SELECT members.*,
a.commenter_id,
b.user_id,
a.comcount,
b.chatcount,
c.totFriend
FROM members
LEFT JOIN
(
SELECT commenter_id,
Count(*) comCount
FROM comments
GROUP BY commenter_id
) a
ON members.id = a.commenter_id
LEFT JOIN
(
SELECT user_id,
Count(*) chatCount
FROM chat_box
GROUP BY user_id
) b
ON members.id = b.user_id
LEFT JOIN
(
SELECT Count(friend_id) AS totFriend,
friend_id AS fi,
logged_user_id AS user,
friend_accepted AS fa
FROM member_friends
WHERE logged_user_id = '1'
AND friend_id = '290'
) c
on members.id = c.friend_id
WHERE members.id = '290'

issue with joins

I have the following query, in which I used JOINs. It says:
unknown column m.bv ..
Could you please take a look and tell me what I'm doing wrong?
$query4 = 'SELECT u.*, SUM(c.ts) AS total_sum1, SUM(m.bv) AS total_sum
FROM users u
LEFT JOIN
(SELECT user_id ,SUM(points) AS ts FROM coupon GROUP BY user_id) c
ON u.user_id=c.user_id
LEFT JOIN
(SELECT user_id ,SUM(points) AS bv FROM matching GROUP BY user_id) r
ON u.user_id=m.user_id
where u.user_id="'.$_SESSION['user_name'].'"
GROUP BY u.user_id';
You are selecting SUM(points) AS bv from the table with the alias r, there is no tables with the alias m. So that it has to be r.bv instead like so:
SELECT
u.*,
SUM(c.ts) AS total_sum1,
SUM(r.bv) AS total_sum
FROM users u
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS ts
FROM coupon
GROUP BY user_id
) c ON u.user_id=c.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS bv
FROM matching
GROUP BY user_id
) r ON u.user_id = m.user_id
where u.user_id="'.$_SESSION['user_name'].'"
GROUP BY u.user_id
Replace m., with r. Look at second Join
You have aliased the derived table with r and you reference that table (twice) with m. Correct one or the other.
Since you group by user_id in the two subqueries and user_id is (I assume) the primary key of table user, you don't really need the final GROUP BY.
I would write it like this, if it was meant for all (many) users:
SELECT u.*, COALESCE(c.ts, 0) AS total_sum1, COALESCE(m.bv, 0) AS total_sum
FROM users u
LEFT JOIN
(SELECT user_id, SUM(points) AS ts FROM coupon GROUP BY user_id) c
ON u.user_id = c.user_id
LEFT JOIN
(SELECT user_id, SUM(points) AS bv FROM matching GROUP BY user_id) m
ON u.user_id = m.user_id
and like this in your (one user) case:
SELECT u.*, COALESCE(c.ts, 0) AS total_sum1, COALESCE(m.bv, 0) AS total_sum
FROM users u
LEFT JOIN
(SELECT SUM(points) AS ts FROM coupon
WHERE user_id = "'.$_SESSION['user_name'].'") c
ON TRUE
LEFT JOIN
(SELECT SUM(points) AS bv FROM matching
WHERE user_id = "'.$_SESSION['user_name'].'") m
ON TRUE
WHERE u.user_id = "'.$_SESSION['user_name'].'"
The last query can also be simplified to:
SELECT u.*,
COALESCE( (SELECT SUM(points) FROM coupon
WHERE user_id = u.user_id)
, 0) AS total_sum1,
COALESCE( (SELECT SUM(points) FROM matching
WHERE user_id = u.user_id)
, 0) AS total_sum
FROM users u
WHERE u.user_id = "'.$_SESSION['user_name'].'"