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 780141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:01:51+00:00 2026-05-14T20:01:51+00:00

I am attempting to create a MySQL snippet that will analyse a table and

  • 0

I am attempting to create a MySQL snippet that will analyse a table and remove duplicate entries (duplicates are based on two fields not entire record)

I have the following code that works when I hard code the variables in the queries, but when I take them out and put them as variables I get MySQL errors, below is the script:

SET @tblname = 'mytable';
SET @fieldname = 'myfield';
SET @concat1 = 'checkfield1';
SET @concat2 = 'checkfield2';

ALTER TABLE @tblname ADD `tmpcheck` VARCHAR( 255 ) NOT NULL;

UPDATE @tblname SET `tmpcheck` = CONCAT(@concat1,'-',@concat2);

CREATE TEMPORARY TABLE `tmp_table` (
`tmpfield` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;

INSERT INTO `tmp_table` (`tmpfield`) SELECT @fieldname FROM @tblname GROUP BY `tmpcheck` HAVING ( COUNT(`tmpcheck`) > 1 );

DELETE FROM @tblname WHERE @fieldname IN (SELECT `tmpfield` FROM `tmp_table`);

ALTER TABLE @tblname DROP `tmpcheck`;

I am getting the following error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@tblname ADD `tmpcheck` VARCHAR( 255 ) NOT NULL' at line 1 

Is this because I can’t use a variable for a table name? What else could be wrong or how would I get around this issue.

  • 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-05-14T20:01:52+00:00Added an answer on May 14, 2026 at 8:01 pm

    Is this because I can’t use a variable for a table name?

    Yes, or for other schema names like columns. String variables can only be used where MySQL expects a '-quoted string.

    If you really need to do this you can with ‘dynamic SQL’: creating your whole query as a string, concatenating the @tblname into the string at that time, and executing the lot using EXECUTE. This is pretty ugly and can lead to SQL-injection if you’re not careful, so avoid it if there is any other option.

    SELECT myfield FROM mytable GROUP BY tmpcheck HAVING ( COUNT(tmpcheck) > 1 )

    This seems problematic to me. Unless myfield has a functional dependency on tmpcheck (which AFAICS it can’t, as tmpcheck is not a primary key), that’s not valid ANSI SQL. MySQL would let you get away with it, but what you would be saying is “for each group of rows sharing a value of tmpcheck, pick the fieldname from one row out of that group at random for later deletion”. Is that really what you want? I would expect you to want to delete all but one of the duplicates.

    Normally you shouldn’t need this kind of complicated procedure to remove duplicates. Just use a DELETE-join:

    DELETE my0
    FROM mytable AS my0
    JOIN mytable AS my1
        ON my1.checkfield1=my0.checkfield1 AND my1.checkfield2=my0.checkfield2
        AND my1.id>my0.id;
    

    This is assuming an id field that is orderable and UNIQUE so that you can decide which row gets to stay (here, the one with the highest id). myfield might be that field, but I can’t tell from context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to write a MySQL query that will return element whose details
I'm attempting to create a table for monitoring purposes using the following script: $w3wppriv
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a CustomControl which will have various properties affected by an
I'm attempting to create a single Controller class to handle all foreseeable surveys that
I'm attempting to create a cascading dropdown based on the Knockout.js Cart example but
I'm attempting to create two tables with a one-to-many relationship. Here are the schemas
I'm (attempting) to write a MySQL stored procedure that parses a large text file.
I am attempting to store files in a MySql database based upon this article
I am attempting to create a table-like structure using divs on my site but

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.