简体   繁体   中英

sql query for multiple conditions in the where clause

im trying to set an echo for multiple conditions on a single where clause ... if this value is found echo something ...if the other value is found echo something ... my code currently keeps echoing all the data in the table when it is only supposed to be echoing a particular set of data based on the stored value in the particular field...

<?php
$query = mysql_query("SELECT *
    FROM   tblfoodoptions JOIN tblhealthconditionfoods 
        ON tblfoodoptions.FoodID = tblhealthconditionfoods.FoodID, tblhealthcondition                                
    WHERE  tblhealthconditionfoods.HealthconditionID = tblHealthcondition.HealthconditionID 
    AND tblhealthcondition.Healthcondition IN ( 'Diabetes Type1' OR 'Diabetes Type2' OR 'General Health' OR 'Pregnant' OR 'Anemia' OR 'High Cholesterol')");

{
    echo "<table width='100%' border='4'>
            <caption>Your Food Options</caption>
            <thead>
                <tr>
                    <th>FoodName</th>
                    <th>Serving_Size</th>
                    <th>Calories</th>
                    <th>Cholestrol</th>
                    <th>Sodium</th>
                    <th>Protein</th>
                    <th>Total_Carbohydrates</th>
                    <th>Total_Fat</th>
                </tr>
            </thead>
        <tbody>";

        while($row = mysql_fetch_array($query))
        {
            echo "<tr>";
            echo "<td>".$row['FoodName']."</td>";
            echo "<td>".$row['Serving_Size']."</td>";
            echo "<td>".$row['Calories']."</td>";
            echo "<td>".$row['Cholesterol']."</td>";
            echo "<td>".$row['Sodium']."</td>";
            echo "<td>".$row['Protein']."</td>";
            echo "<td>".$row['Total_Carbohydrates']."</td>";
            echo "<td>".$row['Total_Fat']."</td>";
            echo "</tr>";
        }
        echo "</tbody></table>";
    }       
?>
IN ( 'Diabetes Type1' OR 'Diabetes Type2' OR 'General Health' OR 'Pregnant' OR 'Anemia' OR 'High Cholesterol')

这部分应该是

IN ( 'Diabetes Type1', 'Diabetes Type2', 'General Health', 'Pregnant', 'Anemia', 'High Cholesterol');

Your IN clause is incorrect. You don't OR the values together, you just list them:

... something IN (1,2,3,4,5)

values in IN conditions is the same as many values divided by OR expresion

so

where field = 10 OR field = 20 OR field = 30 
where field IN (10,20,30)

both these lines are similar

First, I just need to point out that your database setup is not ideal for how you want to query results (select). You are using a relational database... use relationships. I am not suggesting that you don't use joins on tables, but you can reorganize the database structure in a way that puts the data where it is related by using 1:many relationships.

On top of this, you are using PHP to obtain or store data... let the program do the thinking, not the database. Letting a database concatenate data or otherwise unnecessarily do joins on tables or data opens you up for SQL injection by 2nd order (which is very nasty and you don't see it coming), but worse yet is you alter the data that is stored. PHP has many caveats that are at your disposal for organizing data in a concise and related manner. Pull the data from the patients into an object or array using a wrapper, then pull your food choices or other medical info about said condition into a multidimensional arraylist

$foods = array(
                "Ritz Crackers" => array("serving" => "6 crackers", "calories" => 42 ...),
                "Yoplait Vanilla Yogurt" => array("serving" => "2oz", "calories" => 22, ...),
                );

That data is not real data, just an example on how you can use array lists to your advantage.

Once you have your program pull data from the DB, then you can use OOP or even procedural programming in PHP to organize data into displayable reports, written as you want. This leaves you the freedom to have the data in your DB be simplified to codes where possible, which the PHP program code knows what is translated into what. This will make your DB work much less, and give you all the freedom to manipulate the data you want without deprecating, truncating, or otherwise clobbering or destroying data in the DB.

There are plenty of tutorials and manual pages with PHP, SQL, and MySQL on the net, just ask exactly what you need to do on the smallest scale possible, and you typically will find the answer that moves you along.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM