Duplicate column on join - mysql

I'm trying to join three tables, after filtering one down to the most recent entry per user. However, all of a sudden I'm running into the error Duplicate column name 'username'. I need to join on this "duplicate" column. How do I fix this?
SELECT customers.id,customers.name,customers.username,customers.phone,customers.email,radcheck.value as password
FROM customers
RIGHT JOIN radcheck ON customers.username = radcheck.username
LEFT JOIN (
SELECT * FROM radacct INNER JOIN (
SELECT username,MAX(acctupdatetime) AS latest FROM radacct GROUP BY username
) as radrecent
ON radacct.username = radrecent.username
AND radacct.acctupdatetime = radrecent.latest
) as radlatest
ON customers.username = radlatest.username
WHERE radcheck.attribute = 'Cleartext-Password'

In the * you have two columns that are username. You need to qualify one or both of them. Example below:
SELECT
customers.id,customers.name,customers.username,customers.phone,customers.email,radcheck.value as password
FROM customers
RIGHT JOIN radcheck ON customers.username = radcheck.username
LEFT JOIN (
SELECT radrecent.username, latest FROM radacct INNER JOIN (
--^^^^^^^^^
SELECT username,MAX(acctupdatetime) AS latest FROM radacct GROUP BY username
) as radrecent
ON radacct.username = radrecent.username
AND radacct.acctupdatetime = radrecent.latest
) as radlatest
ON customers.username = radlatest.username
WHERE radcheck.attribute = 'Cleartext-Password'

Related

How to fix join error in mysql update query

I had an error in mysql as picture. please help me
sql code is following:
UPDATE
tbl_users AS Users
SET
Users.money_current = Users.money_current +
CASE
WHEN TempTbl.money_info IS NULL
THEN 0
ELSE TempTbl.money_info
END
LEFT JOIN
(SELECT
userId,
SUM(bet_money * bet_rate) AS money_info
FROM
tbl_betting
WHERE ROUND = 'xxx'
AND is_win = 1
GROUP BY userId) AS TempTbl
ON Users.userId = TempTbl.userId
FROM tbl_users AS Users;
This is the correct syntax:
UPDATE tbl_users AS Users
LEFT JOIN (
SELECT userId, SUM(bet_money * bet_rate) AS money_info
FROM tbl_betting WHERE ROUND = '965802' AND is_win = 1
GROUP BY userId
) AS TempTbl ON Users.userId = TempTbl.userId
SET Users.money_current = Users.money_current + COALESCE(TempTbl.money_info, 0)
I also changed that CASE expression with COALESCE().
But I think an INNER JOIN would also work in your case, since the unmatched rows of the LEFT JOIN that you use do not change the value of money_current.

mysql joins on multiple table depending on the value of column

I am using a post table and two type of entities school and user can add something to this table. post table has a post_from field to differentiate whether post is from user or school.
i want to write one query with join to user table or school table depending on post_from field.
SELECT *
FROM post
LEFT JOIN `user`
ON (user.id = post.uid AND post.post_type = 'user' )
LEFT JOIN school_profile
ON (school_profile.id = post.school_id AND post.post_type = 'school')
You can do this with a union :
SELECT post.*
FROM post
INNER RJOIN `user`
ON (user.id = post.uid AND post.post_type = 'user' )
union
SELECT post.*
FROM post
INNER JOIN school_profile
ON (school_profile.id = post.school_id AND post.post_type = 'school')
Alternatively, you can do this :
SELECT *
FROM post
LEFT JOIN `user` ON (user.id = post.uid)
LEFT JOIN school_profile ON (school_profile.id = post.school_id)
WHERE post.post_type IN ('user', 'shcool')

I have a table like below I want the latest entry to be displayed

The query which I am using now is below:
select ur.uid
, ua.user_activity_min_budget
, ua.user_activity_max_budget
, ua.user_activity_bedroom
, ptm.property_type_description
, cm.city_name
, lm.locality_name
, ua.user_activity_datetime
from user_registration ur
join ksl_user_activity ua
on ua.registered_user_uid = ur.uid
and ua.user_activity_uid = ( select max(ua0.user_activity_uid) from ksl_user_activity ua0)
join ksl_locality_master lm
on lm.locality_uid = ua.user_activity_area
join ksl_city_master cm
on cm.city_uid = lm.city_uid
join ksl_property_type_master ptm
on ptm. property_type_uid = ua.user_activity_property_type
where date(ua.user_activity_datet±me) >= '20l7-07-24'
and (lm.city_uid = 1 or lm.city_uid=2)
order
by ur.uid
The raw output s as this image shows:
The data is what I get now but I want the latest entry for uid 3,15,33
The reason why I have done the below is and ua.user_activity_uid=(select max(ua0.user_activity_uid) from user_activity ua0).
ksl_user_activity table has a primary key user_activity_id which has the maximum value for the latest entry but I am not getting any data when I include this in my query.
I also tried and ua.user_activity_uid=(select ua0.user_activity_uid from user_activity ua0 order by ua0.user_activity_uid desc limit 1)
This is also not working.
use max() function and sub-query
select t1.uid from user_activity t1
inner join
(select uid,max(user_activity_datetime) as user_activity_datetime from user_activity group by uid
) as t2 on
t1.user_activity_datetime=t2.user_activity_datetime
and t1.uid=t2.uid

How to select sql data by using 2 join tables with OR condition without duplicate records

I have a mysql select statement as below:-
select user.username, chat.from_user,chat.to_user,chat.message,chat.date_created
from chat join user on (chat.from_user or chat.to_user) in (user.user_id)
where(chat.from_user = 3 or chat.to_user = 3) and chat.chat_id IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
and here is my result
I always get username = admin. My expectation result is username will get correct from / to user.
Please help. Thank you.
SELECT
IF(chat.from_user=3, to_user
, IF(chat.to_user=3, form_user, 0)) AS username,
chat.from_user,chat.to_user,chat.message,chat.date_created
FROM chat
LEFT JOIN user fr_user ON chat.from_user = user.user_id
LEFT JOIN user to_user ON chat.to_user = user.user_id -- since you only want to show the TO_USERNAME, you can remove above line
WHERE (chat.from_user = 3 OR chat.to_user = 3) and chat.chat_id
IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
I think you intend:
select u.username, c.from_user, c.to_user, chat.message, c.date_created
from chat c join
user u
on u.user_id in (c.from_user, c.to_user)
where 3 in (c.from_user, c.to_user) and
c.chat_id in (select max(c2.chat_id)
from chat c2
group by chat_group_id
);

How to check a record belongs to user in MySQL?

I've a SQL Query:
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
I must check, does that ticket belongs to current user. User ID is in t.uid. When it's not that user, just returns column "error" with message "Forbidden". It's possible to do with only MySQL?
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
AND t.uid = [User ID goes here]
This query will only turn up records that belong to the user.
If the record doesn't belong to the user, it will return nothing.