/    Sign up×
Community /Pin to ProfileBookmark

INSERT INTO … (SELECT *)

Background:

I have a table with millions of rows which I want to better manage.
Every new file update, I insert a new dataset and change active=1 to active=0 and set new row to active=1
Problem is that a file can have issues and be resent – meaning that the same day dataset can be duplicated in the database (which hasn’t been an issue until now)

Current Problem:

We’re now extending functionality and trying to select datasets for the last day of month… BUT, due to structure, there is many, many examples of duplicates.
Finding and deleting duplicates is a process I would prefer to avoid… So…

My proposed solution:

Create a unique constraint on customer_id-file_date (Obviously this is a challnge because of all the existing duplicates)
So…
I want to replicate the table structure and then add the unique constraint
Then, select all rows from the old table and use; INSERT INTO … (SELECT * …) ON DUPLICATE UPDATE …

Question:

Does a SELECT * return rows in the order they were inserted?
i.e. How can I be certain that only newer rows will update older rows?

OR – is there a better way?

to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 19.2019 — For the ordering question, you cannot depend on the order rows are returned unless you use an order by clause. If your table has an auto-increment column (typically a primary key integer field?), you could order by it (ascending), and reasonably expect to go from oldest to newest. Or, if you have some sort of created_at column or such that's set to NOW() by default on insert, you could use that, instead (but make sure it's indexed!).
Copy linkTweet thisAlerts:
@php-bgraderauthorNov 20.2019 — Yep, Ok. I have a Primary Key and it Auto Increments so I used that ORDER BY id ASC

Now to test the data
×

Success!

Help @php-bgrader spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...