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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:09:59+00:00 2026-05-23T00:09:59+00:00

I have this query, which should insert an amount if an amount equals or

  • 0

I have this query, which should insert an amount if an amount equals or higher for the same ID doesn’t already exists in the table.

I tried this under MySQL 5.1 and MSSQL 2k5 :

MySQL

INSERT IGNORE INTO test (id, amount)
SELECT 6, 50 FROM test
WHERE NOT EXISTS (SELECT 1 FROM test WHERE amount >= 50 AND id = 6) LIMIT 1

MSSQL

INSERT INTO test (id, amount)
SELECT 6, 50 FROM test
WHERE NOT EXISTS (SELECT TOP 1 1 FROM test WHERE amount >= 50 AND id = 6)

This query works fine IF there is already at least one entry in the table. If the table is empty, it will never work. It’s the same behavior under MySQL (5.1) and MSSQL (2005).
I don’t understand why. Anybody has an explanation and a way to fix this query to work even if the table is completely empty?

EDIT : I need this for MySQL mostly…

UPDATE : I started a new question specifically for MySQL :
MySQL Problem with inserting a row with certain conditions

  • 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-23T00:09:59+00:00Added an answer on May 23, 2026 at 12:09 am

    It fails because the select statement selects values of 6 and 50 based on the number of rows returned. Since your table is empty – no rows are returned to have these values.

    Modify it to

     INSERT INTO test (id, amount) SELECT 6, 50 WHERE NOT EXISTS (SELECT TOP 1 1 FROM test WHERE amount >= 50 AND id = 6) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query to insert a row into a table, which has a
I have a query which attempts to insert a row into a table. It
I have this query which works correctly in MySQL. More background on it here
Right now I have this SQL query which is valid but always times out:
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have this query. It matches anything which has South in its name. But
I have a query which produces a list of orders like this: Invoice Description
So I have this app which needs to query entities from the Azure Tables
I have a table which is full of arbitrarily formatted phone numbers, like this
I have this query statement and want to only get records that has a

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.