I have this SQL query and it is rather advanced for me. I have a table add_design with two columns status and home, so I want to add a part WHERE status=1 and home='Yes' in the query. I just don't understand where to put it. Can someone breakdown the query for me please?
SELECT mytable.*, CONCAT(fname, ' ', lname) AS user_name, user.image AS user_image,
user.fname, user.lname, add_design_doc.image as product_image
FROM
(
SELECT
add_design.id, add_design.user_id, title, category, COUNT(add_design.user_id) AS total_likes
FROM
add_design INNER JOIN
like_unlike ON (add_design.id = like_unlike.product_id AND
like_unlike.type = '1')
GROUP BY
add_design.id
ORDER BY
total_likes DESC ) AS mytable INNER JOIN
user ON mytable.user_id = user.id LEFT OUTER JOIN
add_design_doc ON mytable.id = add_design_id
GROUP BY
user_id LIMIT 4;
Related
I am getting a calculation error in my code
SELECT dep_id,
dept_info.name AS dept_name,
count(dep_id) AS totalInovators,
count(user_id) AS totalIdea,
sum(POINT) AS totalpoint
FROM user_info
JOIN dept_info ON user_info.dep_id =dept_info.id
JOIN user_idea ON user_info.id=user_idea.user_id
GROUP BY dep_id
ORDER BY dep_id DESC
My output result:
Expected result:
With my table user_info :
My user_idea :
My dept_info :
Below the query that solve your problem:
select
user_info.dep_id,
dept_info.name,
count(*) as totalInovators,
sum(ideas_count) as totalIdea,
sum(point) as totalpoint
from
-- first we aggregate table user_idea
(select user_id, count(*) ideas_count from user_idea group by user_id) ideas
-- and after join rest of required tables
join user_info on ideas.user_id = user_info.id
join dept_info on dept_info.id = user_info.dep_id
group by user_info.dep_id, dept_info.name;
Working code here: SQLize.online
I suspect that you are joining along different dimensions. If so, a quick-and-easy solution uses count(distinct):
select d.id, d.name as dept_name, count(distinct u.id) as totalInovators,
count(*) as totalIdea, sum(i.point) as totalpoint
from user_info u join
dept_info d
on u.dep_id = d.id join
user_idea i
on u.id = i.user_id
group by d.id
order by d.id desc
The following query is done after disabling ONLY_FULL_GROUP_BY in MySQL. Now I want the same result without disabling ONLY_FULL_GROUP_BY mode like GROUP BY r.user_id, u.fullname, r.time_taken
SELECT u.fullname, ROUND(AVG(r.correct), 2) avg_correct, date_format(r.time_taken,'%d-%m-%Y') time_taken
FROM (SELECT user_id, concat( first_name, ' ', last_name) fullname from user) u
LEFT JOIN test_result r ON u.user_id = r.user_id
GROUP BY r.user_id
ORDER BY r.time_taken DESC
Screenshot:
Can anyone help me?
You must add u.fullname to the group by clause and this does not affect the results as the user's id and fullname are uniquely grouped together, but in the case of time_taken you must use any_value():
SELECT
u.fullname,
ROUND(AVG(r.correct), 2) avg_correct,
date_format(any_value(r.time_taken),'%d-%m-%Y') time_taken
FROM (SELECT user_id, concat( first_name, ' ', last_name) fullname from user) u
LEFT JOIN test_result r ON u.user_id = r.user_id
GROUP BY r.user_id, u.fullname
ORDER BY time_taken DESC
You can find more here: MySQL Handling of GROUP BY
I would recommend writing this query as:
SELECT CONCAT(u.first_name, ' ', u.last_name) as fullname,
ROUND(AVG(r.correct), 2) as avg_correct,
DATE_FORMAT(MAX(r.time_taken), '%d-%m-%Y') as time_taken
FROM user u LEFT JOIN
test_result r
ON u.user_id = r.user_id
GROUP BY u.user_id, fullname
ORDER BY MAX(r.time_taken) DESC;
Notes:
The subquery in the FROM clause does not help the query. It might impede the optimizer.
Don't GROUP BY columns from the second table in a LEFT JOIN (unless you really know what you are doing). The value would be NULL for non-matches.
MySQL and MariaDB allow column aliases in the GROUP BY clause.
For the ORDER BY to work as you intend, it needs to be on the value before formatting, not after formatting. The format %d-%m-%Y does not order by time.
I'm facing a little problem with mysql where clause.
This is the query:
SELECT u.id user
, p.id product_purchased
, p.name product_name
, pl.store_id store
, COUNT(*) occurrences
, total_spent
, total_product_purchased
, pl.registration
FROM purchases_log pl
JOIN user u
ON pl.user_id = u.id
JOIN product p
ON pl.product_id = p.id
JOIN
( SELECT user_id
, SUM(price) total_spent
, COUNT(product_id) total_product_purchased
FROM purchases_log pl
GROUP
BY user_id
) t1
ON u.id = t1.user_id
WHERE pl.store_id IN (1,2,3)
AND occurrences > 1
GROUP
BY user
, product_name
ORDER
BY u.id ASC
, pl.registration ASC;
This is the output error:
Error Code: 1054. Unknown column 'occurrences' in 'where clause' 0.067 sec
I have already tried assign AS to occurrences or using pl.
So, can someone explain me how to correctly define the result of a count function in where clause?
You need to use HAVING instead of COUNT as group by is applied after WHERE clause and hence, it won't know about any group/aggregate columns, e.g/:
SELECT u.id user,p.id product_purchased, p.name product_name, pl.store_id store, COUNT(*) AS occurrences, total_spent, total_product_purchased, pl.registration
FROM purchases_log pl
JOIN user u ON pl.user_id=u.id
JOIN product p ON pl.product_id=p.id
JOIN (SELECT user_id, SUM(price) AS total_spent,COUNT(product_id) AS total_product_purchased FROM purchases_log pl GROUP BY user_id) t1 ON u.id=t1.user_id
WHERE pl.store_id IN (1,2,3)
GROUP BY user, product_name
HAVING COUNT(*) > 1
ORDER BY u.id ASC, pl.registration ASC;
Update
If a user has more than one product associated then it's good to add all the non aggregate columns in GROUP BY to get all the combinations of user and product. The current query will not return all the combinations.
For further optimization, as #strawberry has suggest, you can run EXPLAIN and see which indices are used and whether there is any need to create any new index.
select
user.name,
(select department.name
from profile, degree, department
where user.profile_id=profile.id
and profile.degree_id=degree.id
and degree.department_id=department.id) DEPT_NAME
from user, query
where user.id=query.user_id as USER_NAME
order by user.name;
This is the query I have written.In this for department.name the column alias changed to DEPT_NAME but for user.name it doesn't changes, it throws error.. Please help...
Move your alias to fields clause:
select
`user`.name as USER_NAME,
(select department.name
from profile, degree, department
where `user`.profile_id=profile.id
and profile.degree_id=degree.id
and degree.department_id=department.id LIMIT 1) DEPT_NAME
from `user`, query
where `user`.id=query.user_id
order by `user`.name;
Also your query is too heavy, try to change it and check difference:
select u.name USER_NAME, dp.name DEPT_NAME
from `user` u
inner join query q on u.id = q.user_id
left join profile p on p.id = u.profile_id
left join degree d on d.id = p.degree_id
left join department dp on dp.id = d.department_id
order by u.name;
try it-
SELECT usr.name User_Name,(SELECT dt.name FROM profile pf,degree dg,department dt WHERE usr.profile_id=pf.id AND pf.degree_id=dg.id AND dg.department_id=dt.id limit 1)DEPT_NAME FROM USER usr, QUERY qry WHERE usr.id=qry.user_id ORDER BY usr.name;
Even you can use below query which can be fast-
SELECT user.name user_name,department.name AS DEPT_NAME
FROM profile,degree,department,`user`,`query`
WHERE user.profile_id=profile.id AND profile.degree_id=degree.id AND degree.department_id=department.id AND user.id=query.user_id ORDER BY user.name;
Getting multiple records from table with subquery joins
SELECT
COUNT(*) AS total_count,
(SELECT
chat_box.user_id,
chat_box.message,
members.id,
members.display_name
FROM chat_box INNER JOIN members
ON chat_box.user_id = members.id
ORDER BY chat_id DESC LIMIT 1),
(SELECT COUNT(DISTINCT user_id) FROM chat_box) AS users_count
FROM chat_box
This is what I have so far, I want to get the members.display_name from the inner join where the chat_box.user_id = members.id as an output along aside the chat_box.message and save members.display_name and chat_box.message to a variable. Any help is appreciated.
It is not exactly clear what you are trying to do, but it seems like you could use something like this:
select u.user_id,
u.message,
u.id,
u.display_name,
cb1.total_count,
cb1.users_count
from
(
SELECT cb.user_id ,
cb.message,
m.id,
m.display_name
FROM chat_box cb
INNER JOIN members m
ON cb.user_id = m.id
) u
CROSS JOIN
(
select COUNT(*) AS total_count,
COUNT(DISTINCT user_id) AS users_count
FROM chat_box
) cb1