1054 error with nested query with right join - mysql

I would like to display a count of tickets per staff member while showing current tickets.
SELECT s.firstname, s.lastname, t.ticket_id, o.id, t.created FROM ost_ticket t
JOIN ost_staff s ON t.staff_id = s.staff_id
JOIN ost_user u ON t.user_id = u.id
JOIN ost_organization o ON u.org_id = o.id
RIGHT JOIN (
SELECT COUNT(tt.ticket_id)
FROM ost_ticket tt) a ON t.ticket_id = a.ticket_id WHERE...
I'm getting a Error in query (1054):
Unknown column 'a.ticket_id' in 'on clause'.

You need to select ticket_id in the subquery if you want to refer to it on the outside.
RIGHT JOIN
(
SELECT ticket_id, COUNT(ticket_id) as cnt
FROM ost_ticket
GROUP BY ticket_id
) a ON t.ticket_id = a.ticket_id

Related

MySQL Group Join Table

I have below three SQL statement and I want to select out like below, I tried but not success.
Need some
help.
Output:
member_id, balance, firstname, lastname, LastPurchase, LastOrde
SELECT c.member_id
, c.firstname
, c.lastname
, m.balance
FROM member m
, customer c
where m.member_id = c.member_id
order
by m.member_id
SELECT member_id, max(date) as LastPurchase
FROM purchase
GROUP
BY member_id
SELECT member_id, max(date) as LastOrder
FROM ordert
GROUP
BY member_id
You can join these statements -
SELECT c.member_id, c.firstname, c.lastname, m.balance, p.LastPurchase, o.LastOrder
FROM member m
join customer c on m.member_id = c.member_id
left join (SELECT member_id, max(date) as LastPurchase
FROM purchase
GROUP BY member_id) p on p.member_id = m.member_id
left join (SELECT member_id, max(date) as LastOrder
FROM ordert
GROUP BY member_id) o on o.member_id = m.member_id
order by m.member_id
You can join the aggregate queries. The JOIN ... USING syntax comes handy here, since all join column names are the same:
SELECT c.member_id, c.firstname, c.lastname, m.balance, p.last_purchase, o.last_purchase
FROM member m
INNER JOIN customer c USING(member_id)
INNER JOIN (
SELECT member_id, max(date) last_purchase FROM purchase GROUP BY member_id
) p USING(member_id)
INNER JOIN (
SELECT member_id, max(date) last_order FROM order GROUP BY member_id
) o USING(member_id)
ORDER BY c.member_id
Important: your original query uses implicit, old-shool joins (with a comma in the from clause) - this syntax fell out of favor more than 20 years ago and its use is discourage, since it is harder to write, read, and understand.
One of the many benefits of using explicit joins here is that you can easily change the INNER JOINs to LEFT JOINs if there is a possibility that a member has no purchase or no order at all.

mysql : calculations on alias fields in mysql

I already referred :
How to use alias as field in mysql
&
Adding MySQL alias fields together
I want to some calculations on alias fields but it throws error
following is my query
select j.*,
(select sum(stars) from ratingstar where jobid=j.id) as stars,
(select count(*) from ratingstar where jobid=j.id) as count,
((stars/(count*5)*5)) as rating //I have problem here if I remove this it works fine
from jobs j inner join proposals p on p.jobid=j.id
inner join us_signup u on u.id=p.userid
inner join hired h on h.proposalid=p.id
where h.status="finished"
But it throws error
error in this select line
select j.*,
(select sum(stars) from ratingstar where jobid=j.id) as stars,
(select count(*) from ratingstar where jobid=j.id) as count,
((stars/(count*5)*5)) as rating //I have problem here if I remove this it works fine
and error is
Fatal error: Uncaught exception 'Exception' with message 'Unknown
column 'stars' in 'field list' query: select j.,(select sum(stars)
from ratingstar where jobid=j.id) as stars,(select count() from
ratingstar where jobid=j.id) as count,((stars/(count*5)*5)) as rating
from jobs j inner join proposals p on p.jobid=j.id inner join
us_signup u on u.id=p.userid inner join hired h on h.proposalid=p.id
where h.status="finished"' in
E:\wamp\www\sugumar\mysuite\includes\classes\MysqliDb.php on line 3637
i think you missed a ) at the end
((stars/(count*5)*5))
edit* you didnt miss it,you just added an useless one at the beginning
(stars/(count*5)*5)
Count is a reserved SQL term, if you want to use is as a field, you should do it like this:
select j.*,
(select sum(stars) from ratingstar where jobid=j.id) as stars,
(select count(*) from ratingstar where jobid=j.id) as count,
((stars/(`count`*5)*5)) as rating //I have problem here if I remove this it works fine
from jobs j inner join proposals p on p.jobid=j.id
inner join us_signup u on u.id=p.userid
inner join hired h on h.proposalid=p.id
where h.status="finished"
Since count is a reserved word/function, you need to quote it using backticks:
select j.*,
(select sum(`stars`) from `ratingstar` where `jobid` = j.`id`) as `stars`,
(select count(*) from `ratingstar` where `jobid` = j.`id`) as `count`,
(`stars` / (`count` * 5) * 5) as `rating`
from `jobs` j
inner join `proposals` p
on p.`jobid` = j.`id`
inner join `us_signup` u
on u.`id` = p.`userid`
inner join `hired` h
on h.`proposalid` = p.`id`
where h.`status` = "finished"

How to recognize parent alias in Join (SELECT?

I get the error below:
ER_BAD_FIELD_ERROR: Unknown column 'c.id' in 'where clause':
SELECT *
FROM clients c
LEFT JOIN
(SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
WHERE client_id = c.id) AS l ON
l.client_id = c.id
I need use WHERE to group smpp_user columns for each c.id from main SELECT.
Help please? I believe it's possible.
Just remove WHERE clause in your sub query and use GROUP BY:
SELECT *
FROM clients c
LEFT JOIN (
SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
GROUP BY client_id
) AS l ON l.client_id = c.id

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'

join two inline tables - unknown column?

I'm getting "Error Code: 1054. Unknown column 'E1.extensao' in 'field list'". I changed table names but my query is as follows:
SELECT E1.stateName, E1.city, E1.boughtEnough, E2.bought
FROM
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING max(n.orders) >= 6) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E1
JOIN (SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
count(n.ordervalue) as bought,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING ((max(n.orders) < 6) AND (count(n.orders) >= 1))) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E2 ON E1.cityId = E2.cityId
I'm more used to SQL Server, not MySQL. What am I getting wrong?
I assume that E1.extensao means E1.boughtEnough? Look closely at how E1 is defined:
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
...) t
...) E1
There's a t.boughtEnough, but you're not "passing it up" to E1.