Mysql查询noobie的帮助(Mysql query noobie assistance)

编程入门 行业动态 更新时间:2024-10-21 06:38:09
Mysql查询noobie的帮助(Mysql query noobie assistance)

你好,我聘请了一个编码器来做我的网站的大部分工作。 但我希望能够做一些基本的事情,我试图使用mysql查询生成器,但仍然无法得到概念大声笑。 所以它可能真的很基本,但我有两张桌子。

$sql = "SELECT distinct count(title) FROM #cookingbooks "; $db->setQuery($sql); $cooking= $db->loadResult(); //var_dump($cooking);die; $sql = "SELECT distinct count(title) FROM #historybooks "; $db->setQuery($sql); $history= $db->loadResult(); //var_dump($history);die;

现在他们中的任何一个都会给我一些记录,但这是我奋斗的地方。 我怎样才能让一个查询来计算这两个表中的记录,并从两个表中给我总数? 作为一个所以,而不是有12000本烹饪书。 和38,000的历史。 我的结果只会得到50,000

Hello i hired a coder to do most my sites work. But i would like to be able to do some basic things i tried to use mysql query builder but still cant get the concept lol. so its probably really basic but i have two tables.

$sql = "SELECT distinct count(title) FROM #cookingbooks "; $db->setQuery($sql); $cooking= $db->loadResult(); //var_dump($cooking);die; $sql = "SELECT distinct count(title) FROM #historybooks "; $db->setQuery($sql); $history= $db->loadResult(); //var_dump($history);die;

now either one of them will get me a count of the records, but this is where i struggle. How can i make one query to count the records from both tables and give me the total from both tables ? as one So instead of having 12,000 books for cooking. and 38,000 for history. i would just get 50,000 as my result

最满意答案

你可以这样做,但你仍然需要多个查询才能做到这一点。 在PHP中添加2个数字可能会更简单,但是在这里您可以:

select (SELECT count(title) FROM #cookingbooks) + (SELECT count(title) FROM #historybooks) as total_books

您可能需要重新考虑数据库结构并拥有一个带有类型字段的books表,其中类型字段可以识别该书是关于历史还是烹饪。 或者通过添加tags和tag_of_books表来创建更复杂的标签系统,其中单个图书可以属于多个类别。 例如,一本名为“烹饪史”的书可以属于这两个类别。

You can do that, but you will still need multiple queries to do that. It may be simpler to add a the 2 numbers together in php, but here you go:

select (SELECT count(title) FROM #cookingbooks) + (SELECT count(title) FROM #historybooks) as total_books

You may want to reconsider yor database structure and have a single books table with a type field, where the type field can identify if the book is about history or cooking. Or have a more complex tagging system by adding a tags and a tag_of_books table, where a single book can belong to multiple categories. E.g. a book called "History of Cooking" can belong to both categories.

更多推荐

本文发布于:2023-04-28 04:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:noobie   Mysql   assistance   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!