codefossa Posted October 31, 2014 Share Posted October 31, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/ Share on other sites More sharing options...
Barand Posted October 31, 2014 Share Posted October 31, 2014 Put the ones that you want to check into a temporary table and use a LEFT JOIN to check for missing ids Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495365 Share on other sites More sharing options...
codefossa Posted October 31, 2014 Author Share Posted October 31, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495366 Share on other sites More sharing options...
Barand Posted October 31, 2014 Share Posted October 31, 2014 If they are already in a table then you won't need to create another. But then I have no idea of what you are starting with so an example would be impossible. Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495369 Share on other sites More sharing options...
Solution Psycho Posted October 31, 2014 Solution Share Posted October 31, 2014 . . . 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" 1 Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495371 Share on other sites More sharing options...
codefossa Posted October 31, 2014 Author Share Posted October 31, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495375 Share on other sites More sharing options...
Psycho Posted October 31, 2014 Share Posted October 31, 2014 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? 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. Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495376 Share on other sites More sharing options...
codefossa Posted October 31, 2014 Author Share Posted October 31, 2014 Good to know. But, with all due respect, what the hell were you even asking in your original post? 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. Quote Link to comment https://forums.phpfreaks.com/topic/292189-check-if-rows-exist/#findComment-1495378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.