简体   繁体   English

MySQL查询noobie协助

[英]Mysql query noobie assistance

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. 但是我希望能够做一些我尝试使用mysql查询生成器的基本操作,但仍然无法理解这个概念。 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. 因此,而不是拥有12,000本书来做饭。 and 38,000 for history. 和38,000个历史记录。 i would just get 50,000 as my result 我只能得到50,000

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: 在php中将两个数字加在一起可能会更简单,但是在这里您可以:

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. 您可能需要重新考虑数据库的结构,并使用带有type字段的单个books表,在type表中,type字段可以标识该书是关于历史还是烹饪。 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. 或者通过添加tagstag_of_books表来拥有更复杂的标签系统,其中一本书可以属于多个类别。 Eg a book called "History of Cooking" can belong to both categories. 例如,一本名为“烹饪史”的书可能属于这两个类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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