0

I receive lists of values like (comma seperator)

D1,D2,D3

and I need to check if each of these values exists in DB or not (check if D1 exists , D2 and D3)

I know I can loop and send "N" queries to DB (but sometimes I receive long list)

is there any automatic way in MySQL to handle this? or in PHP

table to match with:

id     data
1       D1
2       D3
3       D4
4       D5

thanks,

2
  • Please clarify what you need as the output? Because it seams to me that you need an SP at MySQL side that would accept the list of values as an input parameter and return the list of values that doesn't match the values in the table. Or Empty list if all of them match. Commented Oct 27, 2014 at 9:26
  • I need to know if each element in the list (D1 , D2 ...) exists, without fixed return data, just need to check existance of values Commented Oct 27, 2014 at 9:28

4 Answers 4

2

To check if it exists you need to query your database for it, either like this:

SELECT data FROM table WHERE `data` IN ('D1','D2','D3')

or in loop, looking for each value separately (depending on logic of your app)

foreach( ... ) {
   select ....
}
Sign up to request clarification or add additional context in comments.

4 Comments

Asker already said he didn't want to send N queries like your loop suggests.
sometimes what you want is not necessary what should be done. that's why I wrote depending on logic of your app as we do not know what he needs it all for.
Well he wants the good thing since sending multiple queries to the server is less efficient then sending one big query. Keep in mind that for simple queries, most of the time is spent in transit as opposed to actual number crunching.
Sure it is, but we do not know too many things from OP's env so this discussion is quite pointless at this moment. Simply know the possibilities and select what's best.
1

Try this:

SELECT * FROM table WHERE `data` IN ('D1','D2','D3').

1 Comment

Selecting the id column is better since select * is a performance issue for large tables.
1

you can use this query

select * from table where data in ('D1','D2','D3','D4')

6 Comments

Selecting the id column is better since select * is a performance issue for large tables.
Asker wants to search the data column so there is no point in querying for the id since he doesn't know what id to look for. If he has index on data column he should be ok.
@AlexLinte I rephrased my comment.
@Jonast92, yes you're right, limiting the selected columns is ok. But selecting all columns is a performance issue only when you have a huge result.
@AlexLinte Selecting less is better from the beginning, then you can avoid having to change the queries later on when the tables begin to grow.
|
1

This is a snippet you can use in PHP to retrieve the information needed.

// setup database connection
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// define the list of values to check for 
$values_list = "D1,D2,D3";
// explode list into array
$values = explode(',', $values_list);
// initiate empty array for database values
$db_values = array();
// create the query
$query = "SELECT data FROM table WHERE `data` IN ('" . implode("','", $values) . "')";
// run the query
if ($result = mysqli::query($query)) { 
    // loop through results and add values to database value array
    while($obj = $result->fetch_object()){ 
        $db_values[] = $obj->data;
    } 
} 
$result->close();
// go through given values and check if they are in database value array or not, print result
foreach ($values AS $value) {
    if (in_array($value, $db_values)) {
        echo "Value $value exists in database.\r\n";
    }
    else {
        echo "Value $value does not exist in database.\r\n";
    }
}

3 Comments

this is great effort from u @Paul but I'm using Laravel framework, sorry I didn't mention it in question, thanks
@Paul i think mwafi just wants to check for their existance in DB so the query with the initial SELECT should be enough.
@AlexLinte It surely is enough, if he wants to check if ANY of the values is in the DB. But if mwafi wants to know, which values are present or not, you have to go through the results and check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.