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

  • Home
  • SEARCH
  • 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 8318243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:53:41+00:00 2026-06-08T21:53:41+00:00

I have two tables like this in mysql a.cardnumber (unique) a.position (numerical 3 digits

  • 0

I have two tables like this in mysql

a.cardnumber (unique)
a.position (numerical 3 digits or null)
a.serial

b.serial (unique)
b.lastused

I want to update any rows in “a” where position is above 600 AND “a.serial” is blank with any serial from “b.serial” where “b.lastused” is either null or more than 30 days ago. When the serial is copied into “a.serial” I want to update “b.lastused” with today’s date so I know that the relevant “b.serial” has been used today.

There is no relation to the two tables apart from the serial and any serial from b can be used with any cardnumber in a.

I’ve tried this using my limited knowledge of mysql but I keep getting an error from my mysql desktop program to say I have an error in my query 🙁

Any help much appreciated!

  • 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-08T21:53:44+00:00Added an answer on June 8, 2026 at 9:53 pm

    I’m assuming here that you want to use a separate b.serial for each row to be updated in a. (This isn’t specifically stated, but it seems to me to be most likely; please feel free to correct my assumption if it is wrong.)

    I setup a small example. It wasn’t clear what the datatypes for each of the columns, so I used INT where I wasn’t sure. I used DATE datatype (rather than DATETIME) for lastused.

    CREATE TABLE a (`cardnumber` VARCHAR(10) NOT NULL PRIMARY KEY, `position` INT, `serial` INT);
    CREATE TABLE b (`serial` INT NOT NULL PRIMARY KEY, lastused DATE);
    INSERT INTO a VALUES ('x0000',555,NULL),('x0001',700,123),('a1111',601,NULL),('a2222',602,NULL);
    INSERT INTO b VALUES (100,'2012-07-15'),(101,NULL),(102,'2010-01-01'),(103,NULL),(104,NULL);
    SELECT * FROM a;
    SELECT * FROM b;
    

    Based on the conditions you give, the rows with cardnumbers ‘a1111’ and ‘a2222’ should get updated, the other two rows should not (position <= 600, serial already assigned).

    Before we run an UPDATE, we want to first run a SELECT that returns the rows to be updated, along with the values that will be assigned. Once we get that, we can convert that to a multi-table UPDATE statement.

    SELECT a.cardnumber AS `a.cardnumber`
         , a.position   AS `a.position`
         , a.serial     AS `a.serial`
         , b.serial     AS `b.serial`
         , b.lastused   AS `b.lastused`
      FROM (
             SELECT @i := @i + 1 AS i
                  , aa.*
               FROM a aa
               JOIN (SELECT @i := 0) ii
              WHERE aa.position > 600   /* assuming `position` is numeric datatype */
                AND aa.serial IS NULL   /* assuming 'blank' represented by NULL    */
              ORDER BY aa.cardnumber
           ) ia
      JOIN (
             SELECT @j := @j + 1 AS j
                  , bb.serial
                  , bb.lastused
               FROM b bb
               JOIN (SELECT @j := 0) jj
              WHERE bb.lastused IS NULL 
                 OR bb.lastused < DATE_ADD(NOW(),INTERVAL -30 DAY)
              ORDER BY bb.serial
           ) jb
        ON ia.i = jb.j
      JOIN a ON a.cardnumber = ia.cardnumber
      JOIN b ON b.serial = jb.serial
    

    To convert that to an UPDATE, replace the SELECT ... FROM with UPDATE, and add a SET clause to assign new values to the tables.

    UPDATE (
             SELECT @i := @i + 1 AS i
                  , aa.*
               FROM a aa
               JOIN (SELECT @i := 0) ii
              WHERE aa.position > 600
                AND aa.serial IS NULL
              ORDER BY aa.cardnumber
           ) ia
      JOIN (
             SELECT @j := @j + 1 AS j
                  , bb.serial
                  , bb.lastused
               FROM b bb
               JOIN (SELECT @j := 0) jj
              WHERE bb.lastused IS NULL 
                 OR bb.lastused < DATE_ADD(NOW(),INTERVAL -30 DAY)
              ORDER BY bb.serial
           ) jb
        ON ia.i = jb.j 
      JOIN a ON a.cardnumber = ia.cardnumber
      JOIN b ON b.serial = jb.serial
       SET a.serial = b.serial
         , b.lastused = DATE(NOW())
    
    -- 4 row(s) affected
    

    You can run the queries for the inline views seperately (ia, jb) to verify that these are getting the rows you want to update.

    The join from ia to a, and from jb to b, should be on the primary keys unique key.

    The purpose of the ia and jb inline views is to get sequential numbers assigned to those rows so we can match them to each other.

    The joins to a and b are to get back to the row in the original table, which is what we want to update.

    (Obviously, some adjustments need to be made if serial is not an INT, or lastused is a DATETIME rather than a DATE.)

    But this is an example of how I would go about doing the UPDATE you want to do (as best I understood it.)


    NOTE: This approach works with MySQL versions that support subqueries. For MySQL 4.0, you would need to run this in steps, storing the results from the “ia” and “jb” inline views (subqueries) into actual tables. Then reference those tables in the query in place of the inline views. The ii and jj subqueries can be removed, and replaced with separate SELECT @i := 0, @j := 0 statement prior to the execution of the queries that reference these variables.

    • 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 something like this: tool_owners: tool_owners_id | user_id | ...
I have a mysql problem. I have two tables like this that I need
I have two MySQL tables like this: User Id (PK, auto-increment integer) Subscriber Id
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
Let's say I have two tables that look like this: TH TH TH TH
I have two tables which looks something like this Table Queue int ID; string
I have two tables associated by FK. Table student is mapped like this: @Entity
I have two tables Department and Employee . Department table looks like this: ID
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have two tables like this: om_manga: id | link | manganame | viewed

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.