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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:20:14+00:00 2026-05-27T14:20:14+00:00

I have two MySQL tables. What I am trying to do is to export

  • 0

I have two MySQL tables. What I am trying to do is to export the information where Value 1 is 1 less than Value 2 AND where ID_1 does not have its Value 1 and Value 2 equal.

Note:

  • Fields Value 1 and 2 are just integers.
  • Each distinct ID_A has the same Value_2
  • If there are two Value_1s that are one less than Value_2, look to Value_3 and select one that is higher
  • The reason why I have two tables here is because I am going to output information from both tables
  • We can write a script for this, but I need to do this in a single command for bonus points (which my instructor declared is possible)… I haven’t even started a script for this, as I don’t really know how to do that…

tableA looks like this:

ID_1  ID_2
A     A
A     B
B     A
B     B
C     A
C     B
C     C

tableB looks like this:

ID_1  ID_2  Value_1  Value_2  Value_3
A     A     2        3        NULL
A     B     3        3        NULL
B     A     4        5        NULL
B     B     7        5        NULL
C     A     7        8        98
C     B     3        8        NULL
C     C     7        8        56

The query should return this:

ID_1  ID_2
B     A
C     A

Here is what I have so far… And it keeps returning no hits, which is making me confused. I believe it is the AND clause after the first WHERE statement where I need to fix

SELECT CONCAT(...)
  INTO OUTFILE '/tmp/outfile.tab'
       FIELDS TERMINATED BY '\t'
       ESCAPED BY ''
  FROM tableA
 INNER
  JOIN tableB
    ON tableA.ID_1 = tableB.ID_1
   AND tableA.ID_2 = tableB.ID_2
 WHERE tableB.Value_1 - 1 = tableB.Value_2
   AND tableA.ID_1 !=
        ( SELECT DISTINCT
                 ID_1
            FROM tableB
           WHERE ID_1 = tableA.ID_1
             AND Value_1 = Value_2
        )
;

One final note: we issue all commands through putty, in which we can access 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-05-27T14:20:15+00:00Added an answer on May 27, 2026 at 2:20 pm

    I am not 100% sure of the question, but if I get it right, the first row of tableB (Line 25 in https://i.stack.imgur.com/nQLj7.jpg) should NOT be selected, because ID_1=A has Value_1=3 in the second row (Line 26 in https://i.stack.imgur.com/nQLj7.jpg), which is the same as Value_1 of the first row (Line 25 in https://i.stack.imgur.com/nQLj7.jpg).

    So you could start with something like

    SELECT .... FROM
    tableA NATURAL JOIN tableB
    WHERE Value_1=Value_2-1
    AND Value_2 NOT IN (SELECT tb.Value_1 from tableB AS tb WHERE tb.ID_1=tableB.ID_1)
    

    which fullfills requirements #1 and #2. For requirement #3 (if there are two rows for an ID_1, chose the one with the highest Value_3), we need to sort that on Value_3 and wrap it in a superquery for grouping:

    SELECT .... FROM (
    SELECT * FROM
    tableA NATURAL JOIN tableB
    WHERE Value_1=Value_2-1
    AND Value_2 NOT IN (SELECT tb.Value_1 from tableB AS tb WHERE tb.ID_1=tableB.ID_1)
    ORDER BY Value_3 DESC
    ) AS innerview
    GROUP BY Value_1,Value_2
    

    which gives the correct answer for the test data in your example.

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

Sidebar

Related Questions

I have two MySQL tables: Group(gr_id, gr_name, gr_description, parent_id) Group_has_User(User_id, Group_id) I'm trying to
I have two tables in a MySQL database, courses and sessions. I'm trying to
In MySQL I have two tables, tableA and tableB . I am trying to
I have two mysql tables, one containing details on cars and one containing all
i have two mysql tables tableA colA1 colA2 1 whatever 2 whatever 3 whatever
I have two MySQL tables, and I want to find and replace text strings
I have two mysql tables and i want to merge the results of these
I have two tables in a MySql DB: items id (char) name (varchar) description
using php and mysql I have two tables, a users table and a profiles
I have two different tables, lead and click. I would like to query MySQL

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.