PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

[FIXED] DELETE From table Group By

 January 12, 2022     mysql, php, sql, yii     No comments   

Issue

SQL query:

`SELECT * FROM `adl_auth_user` WHERE `user_id` = 1919`

Result is

id  user_id     type_id     type       auth_    role_id     
66428   1919     1                97    103     6

66429   1919    1                 97    104     6

66430   1919    1                 97    105     6

66431   1919    1                 97    106     6

When I Add Group By

SELECT * FROM `adl_auth_user` WHERE `user_id` = 1919 group by type,type_id

Result is

id  user_id     type_id     type    auth_item_id    role_id     

66428   1919    1   97                103             6

I dont need all Details with query

`SELECT * FROM `adl_auth_user` WHERE `user_id` = 1919` (its simple)

How can i delete table details of following

SELECT * FROM `adl_auth_user` WHERE `user_id` = 1919 group by type,type_id

Note : All details are edited format ...


Solution

This query:

DELETE adl_auth_user
FROM adl_auth_user
LEFT JOIN adl_auth_user t2 
   ON adl_auth_user.type_id = t2.type_id AND
      adl_auth_user.type = t2.type AND
      adl_auth_user.id > t2.id
WHERE adl_auth_user.user_id = 1919 AND t2.id IS NOT NULL

deletes all records having the same type_id and type values except from the one having the smallest id value.

Demo here



Answered By - Giorgos Betsos
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing