Find duplicate row get only the older one - mysql

I have this query that will give me those rows that have the field "username" duplicated:
SELECT id, jos_users.username, email, password, lastvisitDate
FROM jos_users
INNER JOIN (SELECT username
FROM jos_users
GROUP BY username HAVING count(id) > 1) dup
ON jos_users.username = dup.username;
I need to get those that have the lastvisitDate lower.
For example:
id | username | email | password | lastvisitDate |
1 | mylogin | | | 2014-10-15 16:42:42 |
2 | mylogin | | | 2014-10-16 16:42:42 |
As you can see, the row with id=1 have the lowest lastvisitDate. How could I put this sentence on the query?
I want this because I'll peform a delete query using this select to delete duplicated rows.

SELECT id, jos_users.username, email, password, lastvisitDate
FROM jos_users
INNER JOIN (SELECT username
FROM jos_users jb
where jb.lastvisitDate =
(select min(jb1.lastvisitDate)
from jos_users jb1
where jb1.username = jb.username)
GROUP BY username
HAVING count(id) > 1) dup
ON jos_users.username = dup.username;

I would probably write this as:
SELECT u.id, u.username, u.email, u.password, u.lastvisitDate
FROM jos_users u
WHERE EXISTS (SELECT 1 FROM jos_users WHERE username = u.username GROUP BY username HAVING count(id) > 1)
AND EXISTS (SELECT 1 FROM jos_users WHERE username = u.username GROUP BY username HAVING min(lastvisitDate) = u.lastvisitDate)

You can get minimum last visit date and do deletion
DELETE J FROM jobusers J
INNER JOIN (
SELECT username, MIN(lastvisitDate) as lastVisitDate
FROM job_users
GROUP BY username HAVING count(id) > 1
)T
ON J.username = T.username
AND J.lastvisitDate = T.lastVisitDate

Related

MySQL select group concat?

I am trying to do a select from two tables. I have two tables:
select * from igw41_users;
+-----+------------+----------+-----------------------------+--------------------------------------------------------------+-------+-----------+---------------------+---------------------+------------+--------------------------------------------------------------------------------------------------------------------------------+---------------------+------------+--------+------+--------------+
| id | name | username | email | password | block | sendEmail | registerDate | lastvisitDate | activation | params | lastResetTime | resetCount | otpKey | otep | requireReset |
17 John Doe AAAAAA nothing else is needed from this table.
select * from igw41_user_profiles;
+---------+----------------------+-----------------------+----------+
| user_id | profile_key | profile_value | ordering |
17 profile.account_type "2" 4
17 profile.postal_code "75055" 1
I need to get id (from igw41_users), username igw41_users , postal_code (from igw41_user_profiles) and account_type (from igw41_user_profiles). But the info I need is in profile_values for each igw41_user_profiles.user_id that matches igw41_users.id.
select id, username, GROUP_CONCAT(profile_value SEPARATOR ',')
from igw41_users
left join igw41_user_profiles on igw41_users.id = igw41_user_profiles.user_id and igw41_user_profiles.profile_key = 'profile.account_type'
group by username;
this gives me the account_type, but I can't figure out how to get the postal_code If I do a right join it gives me: ERROR 1066 (42000): Not unique table/alias: 'igw41_user_profiles'
This is what I tried to get both profile values:
select id, username, GROUP_CONCAT(profile_value SEPARATOR ',')
from igw41_users
left join igw41_user_profiles on igw41_users.id = igw41_user_profiles.user_id and igw41_user_profiles.profile_key = 'profile.account_type'
right join igw41_user_profiles on igw41_users.id = igw41_user_profiles.user_id and igw41_user_profiles.profile_key = 'profile.postal_code'
group by username;
Thanks in advance.
take and igw41_user_profiles.profile_key = 'profile.account_type' out of your first query
If you wish to test for both use in
and igw41_user_profiles.profile_key in('profile.account_type','profile.postal_code')
In common way when you need to join same table more then once you need to use aliases for each join:
select id, username, GROUP_CONCAT(profile_value SEPARATOR ',')
from igw41_users
left join igw41_user_profiles p1
on igw41_users.id = p1.user_id and
p1.profile_key = 'profile.account_type'
right join igw41_user_profiles p2
on igw41_users.id = p2.user_id and
p2.profile_key = 'profile.postal_code'
group by username;
In this case the second join is redundant:
select id, username, GROUP_CONCAT(profile_value SEPARATOR ',')
from igw41_users
left join igw41_user_profiles p1 on igw41_users.id = p1.user_id
where
p1.profile_key = 'profile.account_type' OR
p1.profile_key = 'profile.postal_code'
group by username;

COnditional JOIN query in MySQL

I have two mysql tables
user:
|--------------------------------|
| id | name | type | ruser_type |
|--------------------------------|
| 1 | Admin | a | |
| 2 | | r | c |
|--------------------------------|
customer
|-------------------------|
| id | name | user_id |
|-------------------------|
| 1 | Sam | 2 |
|-------------------------|
If user.type is 'a' or 's', then its admin user whose name is in user table.
If user.type is 'r' and ruser_type is 'c', then its regular user which has a relation in customer table where customer.user_id = user.id
I want a query which would run a conditional join.
If user.type is 'a' or 's', then name would be fetched from user table.
If user.type is 'r' and and ruser_type is 'c', then name would be fetched from customer table with the JOIN condition customer.user_id = user.id.
For this, I have written a query like this:-
SELECT users.fname as adminFname, customers.fname as customerFname, users.type FROM users
LEFT JOIN customers ON (customers.user_id = users.id AND
(
(users.type = 'r' AND users.ruser_type = 'c')
OR users.type = 'a'
OR users.type = 's'
)
)
WHERE users.id = 1
Is there any possibility to optimize the query more?
Also, how can I write this query using Laravel eloquent?
FWIW, I find this marginally easier to read...
SELECT u.fname adminFname
, c.fname customerFname
, u.type
FROM users u
LEFT
JOIN customers c
ON c.user_id = u.id
WHERE u.id = 1
AND (
(u.type = 'r' AND u.ruser_type = 'c')
OR (u.type IN('a','s'))
)
I have written two sql query hope this will help you
SELECT CASE CU.type WHEN 'a' OR 's' THEN CU.name END AS name,
CASE WHEN CU.type = 'r' AND CU.ruser_type = 'c' THEN CR.name END AS cust_name, CU.type
FROM
user AS CU
LEFT JOIN customer AS CR ON CR.user_id = CU.id
In this you'll get result like this,
name cust_name type
Sam r
Admin a
and i have wrote another query like this,
SELECT CASE WHEN CU.type = 'a' OR 's' THEN CU.name ELSE CR.name END AS name, CU.type
FROM
user AS CU
LEFT JOIN customer AS CR ON CR.user_id = CU.id
In this you'll get result like this
name type
Sam r
Admin a
DB File Link
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=b02d931b68a4f70d8b4a84144c60a572
This will give you required result for all users:
SELECT u.fname adminFname
, c.fname customerFname
, u.type
FROM users u
LEFT JOIN customers c
ON (u.type = 'r' AND u.ruser_type = 'c' AND c.user_id = u.id)
Add where condition as required.
You can even simplify it further to get common firstName column in output:
SELECT COALESCE(u.fname, c.fname) firstName, u.type
FROM users u
LEFT JOIN customers c
ON (u.type = 'r' AND u.ruser_type = 'c' AND c.user_id = u.id)

MySQL: Select newest record from table for every user I talked to

I have 2 tables.
users schema:
private_messages schema:
time_sent is a datetime. All other fields are varchars and ints.
sender_id and receiver_id both refer to id in users.
What I want:
Given a users id, say 1, select the last sent message for every other user (whether the given user sent it or the other user sent it). It should return all the columns of the private_messages table along with the other user's username, and ordered by the time_sent.
The functionality I'm trying to achieve is that of when you open up all of your text messages, and you can see a preview of the last message sent between you and everyone else. This query is killing me because of the fact that the given users id can appear in either the sender_id or the receiver_id. Please help me out with writing the query.
Here is exported sql code to create the tables and their contents (just in case you want something to test with):
users
private_messages
private_messages 2
This should do the trick
SELECT U.username,
U2.username,
IF(PM.sender_id = U.id, 'Sent', 'Received') AS `action`,
PM.message,
PM.time_sent
FROM users U
INNER JOIN private_messages PM ON (PM.sender_id = U.id OR PM.receiver_id = U.id)
INNER JOIN users U2 ON (U2.id = IF(PM.sender_id = U.id, PM.receiver_id, PM.sender_id))
WHERE NOT EXISTS (
SELECT *
FROM private_messages NXT_PM
WHERE NXT_PM.time_sent > PM.time_sent
AND (
( NXT_PM.sender_id = PM.sender_id AND NXT_PM.receiver_id = PM.receiver_id )
OR
( NXT_PM.receiver_id = PM.sender_id AND NXT_PM.sender_id = PM.receiver_id )
)
)
AND U.username = 'Kacy'
ORDER BY time_sent DESC
This returns:
+----------+----------+----------+----------+---------------------+
| username | username | action | message | time_sent |
+----------+----------+----------+----------+---------------------+
| Kacy | liz | Sent | hi again | 2015-03-08 10:47:26 |
| Kacy | Jamie | Sent | hi | 2015-03-07 23:01:18 |
| Kacy | tracy | Received | hi | 2015-03-06 12:04:34 |
+----------+----------+----------+----------+---------------------+
3 rows in set (0.02 sec)
You can do as below technique for getting max per group
select
u1.username as sender,
u2.username as receiver,
m.message,
m.time_sent
from private_messages m
inner join users u1 on u1.id = m.sender_id
inner join users u2 on u2.id = m.receiver_id
left join private_messages m1 on m1.sender_id = m.sender_id
and m1.receiver_id = m.receiver_id
and m1.time_sent > m.time_sent
where m.sender_id = 1
and m1.id is null ;
You can also use
and m1.id > m.id
instead of
and m1.time_sent > m.time_sent
if id is auto-incremented in private_messages table
If you need to see data as both sender and receiver for a given user the condition would be as
where ( m.sender_id = 1 or m.receiver_id = 1 ) and m1.id is null
instead of
where m.sender_id = 1
and m1.id is null ;
http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html

MYSQL - Union SELECT & GROUP BY Not working

I have 3 table, log, member, also guest, but my log i stored as customer(user)'s id only, which is either their guest_id or member_id. So here's the problem, because they're from different table, I'm not sure how to join & group together their data.
checkout_log table
id user_id checkout_as
--------------------------------------
1 1 member
2 2 guest
members table
id fullname
--------------------------------------
1 member01
2 member02
guests table
id fullname
--------------------------------------
1 guest01
2 guest02
What I wanted to Achieve - Result
id user_id fullname checkout_as
----------------------------------------------
1 1 member01 member
2 2 guest02 guest
Had tried following sql statement with UNION ALL, or GROUP BY , but had no luck.
SELECT * FROM
(
SELECT checkout_log.id,checkout_log.user_id,guests.fullname,guests.email,checkout_log.checkout_as
FROM checkout_log
LEFT JOIN checkout_product ON checkout_product.checkout_log_id = checkout_log.id
LEFT JOIN guests ON checkout_log.user_id = guests.id
UNION ALL
SELECT checkout_log.id,checkout_log.user_id,members.fullname,members.email,checkout_log.checkout_as
FROM checkout_log
LEFT JOIN checkout_product ON checkout_product.checkout_log_id = checkout_log.id
LEFT JOIN members ON checkout_log.user_id = members.id
) derivedTable
GROUP BY id
Try doing this with joins instead of union
select cl.id, cl.user_id,
coalesce(m.fullname, g.fullname) as fullname,
cl.checkout_as
from checkout_log cl left join
members m
on cl.user_id = m.id and cl.checkout_as = 'member' left join
guests g
on cl.user_id = g.id and cl.checkout_as = 'guest';

Get conversation and last message Query

i'm tring to get the conversations with the relative last message and order them by time and if is read the message. Let's go to show the my logic.
I created 3 table: inbox_join / inbox_msg / users
On the first table "inbox join" i have the datas about who have a active discussion. In this case we have id_user - "1" and id_user_2 - "4" they have a conversation.
On the inbox_msg table I have the text message, id conversation where the message will shown and other field easy to understand.
Inbox join table
Inbox_msg table
Users table
I made a query that work fine, but the my issue is that i can't have the occured_at on the inbox_msg table. I would like find a better solution for have my desidered result and i can't order how i'm looking for.
This is my query
SELECT DISTINCT (
inbox_join.id_conversation
), user_chat.name AS name_conv, user_chat.surname AS surname_conv, user_chat.username as username_conv, user_chat.id as id_chat, image_upload.name_image, (
SELECT DISTINCT (
message
)
FROM inbox_msg
WHERE inbox_join.id_conversation = inbox_msg.id_conversation
ORDER BY occured_at DESC
LIMIT 1
) AS last_msg, users.name, users.surname
FROM inbox_join
INNER JOIN users ON users.id = inbox_join.id_user
INNER JOIN users AS user_chat ON user_chat.id <> 1 AND (inbox_join.id_user_2 = user_chat.id || inbox_join.id_user = user_chat.id)
INNER JOIN image_upload ON image_upload.id_image = user_chat.profile_image
WHERE inbox_join.id_user = 1
OR inbox_join.id_user_2 = 1
Result desidered selecting the conversation about user 1:
id_conversation | id_user | name | surname | username | last_msg | occured_at_last_msg | read_msg |
1 4 E S E Yes 1380724676 0
4 5 G E K Good 1380724675 0
Query:
SELECT im.id_conversation,
im.id_user,
u.name,
u.surname,
u.username,
im.message AS last_msg,
im.occured_at AS occured_at_last_msg,
im.read_msg
FROM inbox_msg im
JOIN users u
ON u.id_user = im.id_user
JOIN (SELECT id_conversation,
MAX(occured_at) AS occured_at
FROM inbox_msg
GROUP BY id_conversation) im2
ON im2.id_conversation = im.id_conversation
AND im2.occured_at = im.occured_at
ORDER BY im.occured_at DESC
I made this query and should work fine, I would like receive comment about this query.
SELECT DISTINCT (
im.id_conversation
), users.name, users.surname, users.username, image_upload.name_image, im.message as last_msg, im.occured_at, im.read_msg
FROM inbox_join
INNER JOIN (
SELECT sub . *
FROM (
SELECT DISTINCT (
id_conversation
), id_user, message, occured_at, read_msg
FROM inbox_msg
WHERE id_user <> 1
ORDER BY occured_at DESC
) AS sub
GROUP BY sub.id_user
ORDER BY sub.occured_at DESC
) AS im ON im.id_conversation = im.id_conversation
INNER JOIN users ON im.id_user = users.id
INNER JOIN image_upload ON users.profile_image = image_upload.id_image
WHERE inbox_join.id_user = 1 || inbox_join.id_user_2 = 1