Jump to content

Check If Rows Exist


Go to solution Solved by Psycho,

Recommended Posts

Is it possible to check if multiple rows exist at the same time.  For example, if a row with the id "1" and another row with id "2" exists, and return something like true then false if only the first exists?

 

I have a file that provides tens of thousands to check and it takes forever.  I know this sounds bad off the start, but it's not something that is run normally.  Only to update my database with new additions.

 

Is there any good way to check a lot at once?  I know you can insert many at a time and save a lot of time.

Link to comment
https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/
Share on other sites

It's set up with (id, ip, name) and I need to check that the ip and name combination don't exist before inserting.  I do this in one query (kind'a) but I would like to check more of them at once.  It sounds like you're saying to insert them all into another table, but that would add more time to this I would think?  Maybe an example of what you mean would help me understand.

Link to comment
https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495366
Share on other sites

  • Solution

 . . .  I need to check that the ip and name combination don't exist before inserting.

 

That doesn't sound like anything to do with what you originally asked. Perhaps you need to provide some examples of what you are talking about. But, if your problem is that you need to prevent duplicates with respect to two fields, you can enforce that directly through the DB schema. For example, if you have fields for First Name and Last Name you can set the schema up so that the combination of First name and Last Name are unique.

 

Here is an example:

 

First Name | Last Name
-------------------------
David        Smith
David        Andeerson
Richard      Smith

 

If you set the combination of First Name + Last Name must be unique, all those values would be allowed. But, you could not add another record such as "David Smith"

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495371
Share on other sites

Sorry for not saying it clearly but you've given me my answer better than I ever expected.  I didn't know you could use a combination of columns with UNIQUE so now, I can use that with INSERT INGORE and it's perfect. :)

 

Thanks so much.

Link to comment
https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495375
Share on other sites

Sorry for not saying it clearly but you've given me my answer better than I ever expected.  I didn't know you could use a combination of columns with UNIQUE so now, I can use that with INSERT INGORE and it's perfect. :)

 

Good to know. But, with all due respect, what the hell were you even asking in your original post? :confused:  :happy-04:  I've reread it a couple of times and it still doesn't make any sense as to how it applies to what I now understand you were trying to accomplish.

Link to comment
https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495376
Share on other sites

Good to know. But, with all due respect, what the hell were you even asking in your original post? :confused:  :happy-04:  I've reread it a couple of times and it still doesn't make any sense as to how it applies to what I now understand you were trying to accomplish.

I'll just say exactly what I was doing then.  I have a table.

 

id - timestamp - ip - user

 

I get anywhere from a couple hundred to a 100,000 to add to it at any time.  I need to check if the ip/user combo exists before adding it.  The way I was doing it was sadly extremely slow because I wasn't aware you could do this, so I had to check if it existed the long way.

 

So, you didn't really answer my question, but gave me an awesome alternative because now I'm able to just add them and it will take care of checking for duplicates.  I guess that's probably where the confusion was because you went in a bit of another direction and it happened to be exactly what I needed.

Link to comment
https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495378
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.