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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:35:57+00:00 2026-06-09T15:35:57+00:00

I have a few groups of data. Each group has a some property field.

  • 0

I have a few groups of data. Each group has a some property field.
For example:

_________________________
| id  | value | property |  
--------------------------
| 1    |  2    |    3    |
--------------------------
| 2    |  2    |    3    |
--------------------------
| 3    |  2    |    3    |
--------------------------
| 4    |  2    |    4    |
-------------------------
| 5    |  2    |    4    |
--------------------------
| 6    |  2    |    4    |
--------------------------

How can I update two strings ordered by id ASC with property = 3, and 2 strings ordered by id ASC with property = 4 by one query?

I want to update 2 of 3 rows with property = 3 and update 2 of 3 rows with property = 4. For example: rows with id 1 and 2, and rows with id 4 and 5
i.e. i want update groups of data with different conditions by one query

  • 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-09T15:35:59+00:00Added an answer on June 9, 2026 at 3:35 pm

    Here’s the solution, and see discussion following:

    update 
      t,
      (select GROUP_CONCAT(ids) as matching_ids from (
        select 
          SUBSTRING_INDEX(GROUP_CONCAT(id order by id), ',', 2) AS ids
        from
          t
        where
          property in (3,4)
        group by
          property
        ) s1
      ) s2
    set value=12345
    where 
      FIND_IN_SET(id, matching_ids) > 0
    ;
    

    To illustrate, and assuming your table is called t, and the initial state is:

    root@mysql-5.1.51> select * from t;
    +----+-------+----------+
    | id | value | property |
    +----+-------+----------+
    |  1 |     2 |        3 |
    |  2 |     2 |        3 |
    |  3 |     2 |        3 |
    |  4 |     2 |        4 |
    |  5 |     2 |        4 |
    |  6 |     2 |        4 |
    +----+-------+----------+
    

    The result of running this query is:

    root@mysql-5.1.51> select * from t;
    +----+-------+----------+
    | id | value | property |
    +----+-------+----------+
    |  1 | 12345 |        3 |
    |  2 | 12345 |        3 |
    |  3 |     2 |        3 |
    |  4 | 12345 |        4 |
    |  5 | 12345 |        4 |
    |  6 |     2 |        4 |
    +----+-------+----------+
    

    A brief explanation of the query:

    I pick up the first two ids for each property using the SUBSTRING_INDEX(GROUP_CONCAT(id order by id), ',', 2) statement.

    I combine the above using GROUP_CONCAT(ids) as matching_ids to get all valid ids.

    Finally, I update all rows in the table where the id is within combined matching_ids text.

    Notes:

    • You should verify your group_concat_max_len variable is long enough. Default is 1024. You most probably want to have this in the millions, anyhow (regardless of my answer).

    • The query is far from being optimal. It answers your question, but you can’t have an optimal query here.

    • You are most probably better off with a transaction containing two or three queries.

    Good luck!

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

Sidebar

Related Questions

First of all i have to clear few things 1.This is not spam group
I have to fetch about few hundreds records from CoreData and group them by
I have a dropdown list with a few options and a group of checkboxes
have written a stochastic simulation in Java, which loads data from a few CSV
i have database table with few text fields which i use to filter data
I have a blog.Few articles inside.Per each article I have a lot of comments
I have unevenly distributed data (wrt date ) for a few years (2003-2008). I
I have few tables that gather data for internal use but i want to
I have a few challenges I need help on. I need to pull data
I have a data file in which each line represents a single record and

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.