Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:50:22+00:00 2026-06-07T10:50:22+00:00

Suppose I have this table: drop table if exists t_user; CREATE TABLE t_user (usr

  • 0

Suppose I have this table:

drop table if exists t_user;
CREATE TABLE t_user (usr int, grp int);
INSERT INTO t_user VALUES (123456, 5);
INSERT INTO t_user VALUES (111111, 5);
INSERT INTO t_user VALUES (111311, 5);
INSERT INTO t_user VALUES (122111, 5);
INSERT INTO t_user VALUES (111111, 5);
INSERT INTO t_user VALUES (111211, 5);
INSERT INTO t_user VALUES (113211, 5);
INSERT INTO t_user VALUES (115311, 5);
INSERT INTO t_user VALUES (122253, 3);
INSERT INTO t_user VALUES (222331, 6);
INSERT INTO t_user VALUES (118811, 3);
INSERT INTO t_user VALUES (112111, 6);
INSERT INTO t_user VALUES (213111, 6);
INSERT INTO t_user VALUES (343321, 7);
INSERT INTO t_user VALUES (114431, 2);
INSERT INTO t_user VALUES (115111, 8);
INSERT INTO t_user VALUES (324353, 3);

I want to run a query like this:

SELECT distinct @u := usr FROM t_user WHERE grp < 6;

To hold the ids of a bunch of users and use the result on a delete statement that will delete all those users:

delete from t_user where usr in ( @u );

The problem is that each time i run these two statements the first listed user is deleted and not all of them as I wanted.

EDIT

I can’t simply run everything on a single query. I really want to know how to do it separately.

EDIT²

I followed @juergen instructions and tried using Temporary tables:

CREATE TEMPORARY TABLE t_temp (id int);

INSERT INTO t_temp SELECT DISTINCT usr FROM t_user WHERE grp < 6;

DELETE FROM t_user WHERE usr IN ( SELECT * FROM t_temp );

DROP TABLE t_temp;

I searched and found some similar answers, but i don’t want to build a string and use it on my delete statement. I want something simpler.

  • mysql using user-defined variable in where clause
  • MySQL: Using variable on a WHERE IN clause
  • In MySQL how do I use a user defined variable in a WHERE IN clause?

How to store the results from a query into a temporary table:
– Create table variable in MySQL

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:50:24+00:00Added an answer on June 7, 2026 at 10:50 am

    just do it in one query

    delete from t_user 
    where usr in (SELECT distinct usr FROM t_user WHERE grp < 6)
    

    or even shorter

    delete from t_user WHERE grp < 6
    

    Edit

    A variable can only hold one entry. You could use a temp table to store the data you want.

    Edit 2

    You could try a join instead of subquery. Haven’t tried it though:

    delete from t_user u1
    inner join t_user u2 on u1.usr = u2.usr 
    WHERE u2.grp < 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose I have this table parent | child 1 2 1 3 2 4
Suppose I have this table: id | name | city ------------------ 1 | n1
Suppose you have a table like this: id user score It's a dump of
A newbie question,really. Suppose I have a html table like this: <div id=div1> <table
Suppose I have the code like this: <table> <tr> <th>name</td> <th>price</td> </tr> <tr> <td>a</td>
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
suppose I have this table id | cash 1 200 2 301 3 101

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.