php sql 将多个查询合并为一个

发布于 2024-12-24 21:32:49 字数 564 浏览 1 评论 0原文

我的页面的一部分有很多小的查询,总共大概有 6 个,从不同的表中获取数据。举个例子:

$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);

如果这些都在一个查询中获取以将行程保存到数据库会更好吗? 1 个查询比 6 个查询要好,不是吗?

是某种 JOIN 或 UNION 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜无邪 2024-12-31 21:32:49

这与查询数量无关,而是与您传输的有用数据量有关。如果您在本地主机上运行数据库,最好让 sql 引擎来解决查询,而不是在其他程序中计算结果。如果您正在考虑谁应该更忙,也是如此。 Apache 或 mysql :)

当然你可以使用一些条件:

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.

来自 MYSQL Bible 的示例:)

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 :)

薔薇婲 2024-12-31 21:32:49

如果您确实想减少查询数量,可以将前两个放在一起,如下所示:

$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;    
}

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;    
}
〆一缕阳光ご 2024-12-31 21:32:49

连接表的想法是这些表之间应该有一些东西(例如关系)。

UNION SELECTS 也是如此,最好避免使用。

如果您希望您的解决方案将来干净且可扩展,我建议您使用 mysqli,而不是 PHP 的 mysql 模块。

请参阅:mysqli::multi_query。有 OOP 变体,您可以在其中创建 mysqli 对象并将函数作为方法调用。

然后,您的查询应如下所示:

// 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

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
请止步禁区 2024-12-31 21:32:49

查询越少(通常情况下,并不总是)越好,但这也意味着保持代码足够清晰,以便其他人可以理解查询。例如,在您提供的代码中,将前两个放在一起,将最后一个分开。

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

上面的代码将返回两行,每行包含投票值(0 或 1)以及该值的投票计数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文