简体   繁体   中英

php sql multiple queries into one

Part of my page I have lots of small little queries, probably about 6 altogether, grabbing data from different tables. As an example:

$sql_result = mysql_query("SELECT * FROM votes WHERE voted_on='$p_id' AND vote=1", $db); 
$votes_up = mysql_num_rows($sql_result);

$sql_result = mysql_query("SELECT * FROM votes WHERE voted_on='$p_id' AND vote=0", $db);
$votes_down = mysql_num_rows($sql_result);

$sql_result = mysql_query("SELECT * FROM kids WHERE (mother_id='$p_id' OR father_id='$p_id')", $db); 
$kids = mysql_num_rows($sql_result);

Would it be better if these were all grabbed in one query to save trips to the database? One query is better than 6 isn't it?

Would it be some kind of JOIN or UNION?

Its not about number of queries but amount of useful datas you transfer. If you are running database on localhost, is better to let sql engine to solve queries instead computing results in additional programs. The same if you are thinking about who should be more bussy. Apache or mysql :)

Of course you can use some conditions:

SELECT catName,
 SUM(IF(titles.langID=1, 1, 0)) AS english,
 SUM(IF(titles.langID=2, 1, 0)) AS deutsch,
 SUM(IF(titles.langID=3, 1, 0)) AS svensk,
 SUM(IF(titles.langID=4, 1, 0)) AS norsk,
COUNT(*)
FROM titles, categories, languages
WHERE titles.catID = categories.catID
AND titles.langID = languages.

example used from MYSQL Bible :)

If you really want to lower the number of queries, you can put the first two together like this:

$sql_result = mysql_query("SELECT * FROM votes WHERE voted_on='$p_id'", $db); 
while ($row = mysql_fetch_array($sql_result)) 
{
extract($row);
if ($vote=='0') ++$votes_up; else ++$votes_down;    
}

The idea of joining tables is that these tables are expected to have something in between (a relation, for example).

Same is for the UNION SELECTS, which are prefered to be avoided.

If you want your solution to be clean and scalable in future, I suggest you to use mysqli, instead of mysql module of PHP.

Refer to: mysqli::multi_query . There is OOP variant, where you create mysqli object and call the function as method.

Then, your query should look like:

// I use ; as the default separator of queries, but it might be different in your case.
// The above could be set with sql statement: DELIMITER ;



$query = "
    SELECT * FROM votes WHERE voted_on='$p_id' AND vote=1;
    SELECT * FROM votes WHERE voted_on='$p_id' AND vote=0;
    SELECT * FROM kids WHERE (mother_id='$p_id' OR father_id='$p_id');
";

$results = mysqli_multi_query($db, $query); // Returns an array of results

Fewer queries are (generally, not always) better, but it's also about keeping your code clear enough that others can understand the query. For example, in the code you provided, keep the first two together, and leave the last one separate.

$sql_result = mysql_query("SELECT vote, COUNT(*) AS vote_count 
    FROM votes
    WHERE voted_on='$p_id'
    GROUP BY vote", $db);

The above will return to you two rows, each containing the vote value (0 or 1) and the vote count for the value.

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