Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A In MySQL is there a limit to the number of keys in a IN() clause?

I'll readily admit I'm not too familiar with MySQL specifically, but personally, I would try to avoid listing all the primary key values in an ad-hoc query. What I would rather do personally is to...

posted 3y ago by Canina‭

Answer
#1: Initial revision by user avatar Canina‭ · 2020-11-13T07:58:57Z (over 3 years ago)
I'll readily admit I'm not too familiar with MySQL specifically, but personally, I would try to avoid listing all the primary key values in an ad-hoc query.

What I would **rather do** personally is to run a separate query to select the rows to update, and then include a condition that the rows to be updated are those that exist in that set of rows.

Something not entirely dissimilar to

    UPDATE example_table
    SET column_A = 1
    WHERE primary_key_column IN (
        SELECT primary_key_column
        FROM example_table
        WHERE <bunch of complex conditions>
    )

Of course, in this *particular* case, you could just as well stick the `WHERE` clause from the subquery in the `UPDATE` statement itself, but that gets somewhat more complex if the query to find the rows to update is more complex than a plain SELECT from the same table that you're updating; say, it's actually selecting from a different table, with its own joins, maybe a union or two, and so on.

If you need to use the same set of key values multiple times and the query to find them is non-trivial, stick them into a temporary table that you `SELECT` from (or join against) instead, and put all of that into a stored procedure.