Duplicate column name 'user_id' problem - mysql

I'm having a problem trying to count the number of user records according to the user's id, however I'm using a subquery to join 2 tables that one has a count parameter but I get an error saying duplicate column name 'user_id.
The query:
$sql = "SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM locations loc
LEFT JOIN
(
SELECT usr.*,
loc.*
FROM
(
members usr
INNER JOIN locations loc
)
WHERE usr.user_id = " . $user_id . "
AND usr.account_disabled = 0
ORDER BY loc.submit_date DESC
) usr ON (loc.user_id = usr.user_id)";
All I need it is to return the user's info and the total_records count done by the COUNT function.
Cheers.
EDIT:
This is what I get returned for this SQL:
SELECT loc.location_id,
loc.street_name,
loc.city,
loc.state,
loc.county,
loc.country,
usr.user_id,
usr.username,
COUNT(loc.location_id) AS total_records
FROM locations loc
INNER JOIN members usr ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id

It's not exactly clear why you've got the derived resultset or the LEFT JOIN. Try this:
SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM LOCATIONS_TABLE loc
INNER JOIN MEMBERS_TABLE usr
ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id

I think the problem is this part:
SELECT usr.*,
loc.*
Both tables have a user_id

Related

MySQL Multiple INNER JOIN + GROUP BY not working as expected

I am generating a Member Leaderboard based on some field values.. Using Below SQL i am getting those values...
SELECT ue1.user_id,
ue1.meta_value coins ,
ue3.meta_value user_rank ,
ue2.meta_value gems,
COUNT(ue4.user_id) quiz_count,
COUNT(ue5.user_id) video_watch_count,
ue1.meta_value + ue3.meta_value + ue2.meta_value total_points
FROM `wp_usermeta` as ue1
INNER JOIN `wp_usermeta` as ue2 ON ue2.user_id = ue1.user_id
AND ue1.meta_key = '_coin_points'
AND ue2.meta_key = '_gem_points'
INNER JOIN `wp_usermeta` as ue3 ON ue3.user_id = ue1.user_id
AND ue3.meta_key = '_user_rank'
INNER JOIN `wp_gamipress_user_earnings` as ue4 ON ue4.user_id = ue1.user_id
AND ue4.post_type = 'quiz-game-master'
INNER JOIN `wp_gamipress_user_earnings` as ue5 ON ue5.user_id = ue1.user_id
AND ue5.post_type = 'video-game-master'
WHERE ue1.meta_value > 0
AND ue2.meta_value > 0
GROUP BY ue1.user_id, ue1.meta_value, ue2.meta_value, ue3.meta_value, ue4.user_id, ue5.user_id
ORDER BY total_points DESC LIMIT 0, 50
What i am getting is... Not Expected Query Result
See? video_watch_count is copying quiz_count value.. I don't know why? Would appreciate if you could help me out here.
Check you join statement. For both quiz_count, video_watch_count, you select data from table wp_gamipress_user_earnings. For each entry if join condition satisfied it will return user_id from corresponding table.
Finally you group it using user_id which is common to both join results.That's why you get same count.

Mysql how can i sql two tables and count groupid from the second

I'm trying to make an Sql , that get all data from first table "posts_main" and then get the count of comments from second table "posts_comments"
I tried:
$sql = "SELECT * FROM posts_main, count(posts_comments.groupid)
INNER JOIN posts_comments ON posts_comments.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'
GROUP BY posts_main.id";
Please, how can i do that ?
thanks....
Try with below query.
$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_group
from posts_main
WHERE posts_main.user_id = '$user_id' ";
Try this with subquery
SELECT posts.*,cnt FROM posts_main
INNER JOIN (select posts_comments.groupid,count(posts_comments.groupid) as cnt
group by posts_comments.groupid)a
ON a.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'

Mysql Select unique record based on multiple columns and display only group and sum amount

Hi I am trying to query a table that conatains multiple duplicates on Code,Amount and Status How will I do this if I only one to get a result group according to the client_group name and get the sum of amount under that group
SELECT `client`.`client_group`
, FORMAT(SUM(`Data_result`.`Data_result_amount` ),2) as sum
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
GROUP BY `client`.`client_group`
Result of said query:
Table
Try to distinct before run the 'sum' check whether this solve your problem
SELECT `client_group` , FORMAT(SUM(`Data_result_amount` ),2) as sum from (
SELECT DISTINCT `client`.`client_group` , `Data_result`.`Data_result_amount`
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
) T
GROUP BY `client_group`
you can check the query here http://sqlfiddle.com/#!9/36a3f8/6

SQL query returns column with data from another column

I have a table Notices connected to tables Likes and Comments. When I return the notices for a user I also create columns: number_of_likes, number_of_comments and liked_by_me. The query is working correctly when the user making the query hasn't liked a notice (liked_by_me = 0) . But if they have (liked_by_me = 1) the value I get for number_of_likes is wrong and is the same as number_of_comments.
Example:
1)
- liked by me = false
- likes = 1
- comments = 5
Returned values:
- liked_by_me = 0
- number_of_likes = 1
- number_of_comments = 5
2)
- liked by me = true
- likes = 2
- comments = 5
Returned values:
- liked_by_me = 1
- number_of_likes = 5
- number_of_comments = 5
Here is the query I am using:
SELECT notices.*
, count(comment.id) as number_of_comments
, count(like1.user_id) as number_of_likes
, like2.user_id IS NOT NULL AS liked_by_me
, boards.name as board_name
FROM notices
LEFT JOIN comments as comment
ON (comment.notice_id = notices.id)
LEFT JOIN likes as like1
ON (like1.notice_id = notices.id)
LEFT JOIN likes as like2
ON (like2.notice_id = notices.id
AND like2.user_id = $1)
LEFT JOIN boards
ON (boards.id = notices.board_id)
LEFT OUTER JOIN board_users
ON (board_users.board_id = notices.board_id)
WHERE board_users.user_id = $1
GROUP BY notices.id
, boards.name
, like2.user_id
, userId
Any help would be appreciated. I have been on this for hours and I don't think I will be able to find the problem.
Thanks!
Solution:
Here is the working query
SELECT notices.*,
(SELECT COUNT(user_id) from likes WHERE likes.notice_id = notices.id) AS number_of_likes,
(SELECT user_id IS NOT NULL from likes WHERE likes.notice_id = notices.id AND likes.user_id = $1) AS liked_by_me,
count(comments.id) as number_of_comments, boards.name as board_name
FROM notices LEFT JOIN comments ON (comments.notice_id = notices.id)
LEFT JOIN boards ON (boards.id = notices.board_id)
LEFT OUTER JOIN board_users ON (board_users.board_id = notices.board_id)
WHERE board_users.user_id = $1 GROUP BY notices.id, boards.name", user);
You will have to use subeselects.
Excellent article on this problem: The GROUPing pitfall
TL;DR: Basically, you have to realize, that all your comments and likes are being multiplicated by one another. Try to display the result of the query without the group clause to see, that duplicate likes/comments are being counted.
EDIT: I didn't test this, but it's how the query might look:
(that is if user can only like one notice once, otherwise you would have to group current user likes too)
SELECT
notices.*,
comments.number_of_comments,
likes.number_of_likes
current_user_likes.user_id IS NOT NULL AS liked_by_me
boards.name AS board_name
FROM notices
LEFT JOIN (
SELECT
COUNT(*) AS number_of_comments,
notice_id
FROM comments
GROUP BY notice_id
) AS comments ON comments.notice_id = notices.id
LEFT JOIN (
SELECT
COUNT(*) AS number_of_likes,
notice_id
FROM likes
GROUP BY notice_id
) AS likes ON likes.notice_id = notices.id
LEFT JOIN likes AS current_user_likes
ON current_user_likes.notice_id = notices.id
AND current_user_likes.user_id = $1
LEFT JOIN boards ON boards.id = notices.board_id
INNER JOIN board_users
ON board_users.board_id = notices.board_id
AND board_users.user_id = $1;

How to optimise query

This query returns all friends of logged in user, which is present in "network" table in below query. This maps frd_id and mem_id from these two tables.
I have optimized this query. but still it is taking time to get result.
Please suggest me optimized query for the same.
SELECT m.mem_id,m.profilenam,m.gender,m.photo_thumb,m.profile_type,n.frd_id as mem_id
FROM `network` n
left join members m on(n.frd_id=m.mem_id)
where ((n.frd_id = m.mem_id and n.mem_id=$uid)
or (n.mem_id = m.mem_id and n.frd_id=$uid))
AND m.ban='n'
AND m.deleted<>'Y'
and profilenam like '%'
Try this:
SELECT m.mem_id,m.profilenam,m.gender,m.photo_thumb,m.profile_type,m.mem_id
FROM `network` n
JOIN members m ON n.frd_id = m.mem_id
AND ( n.mem_id = $uid
OR ( n.mem_id = m.mem_id
AND n.frd_id = $uid
)
)
WHERE m.profilenam IS NOT NULL
AND m.ban = 'n'
AND m.deleted <> 'Y';