php sql 将多个查询合并为一个
我的页面的一部分有很多小的查询,总共大概有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这与查询数量无关,而是与您传输的有用数据量有关。如果您在本地主机上运行数据库,最好让 sql 引擎来解决查询,而不是在其他程序中计算结果。如果您正在考虑谁应该更忙,也是如此。 Apache 或 mysql :)
当然你可以使用一些条件:
来自 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:
example used from MYSQL Bible :)
如果您确实想减少查询数量,可以将前两个放在一起,如下所示:
If you really want to lower the number of queries, you can put the first two together like this:
连接表的想法是这些表之间应该有一些东西(例如关系)。
UNION SELECTS 也是如此,最好避免使用。
如果您希望您的解决方案将来干净且可扩展,我建议您使用 mysqli,而不是 PHP 的 mysql 模块。
请参阅:mysqli::multi_query。有 OOP 变体,您可以在其中创建 mysqli 对象并将函数作为方法调用。
然后,您的查询应如下所示:
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:
查询越少(通常情况下,并不总是)越好,但这也意味着保持代码足够清晰,以便其他人可以理解查询。例如,在您提供的代码中,将前两个放在一起,将最后一个分开。
上面的代码将返回两行,每行包含投票值(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.
The above will return to you two rows, each containing the vote value (0 or 1) and the vote count for the value.