Mysql 统计3个表,并在其中一个表中签入现有ID

Mysql 统计3个表,并在其中一个表中签入现有ID,mysql,join,count,Mysql,Join,Count,第一次查询;两个表都包含所有类别\u ids SELECT * FROM categories c, categories_description cd WHERE c.categories_id = cd.categories_id ORDER BY sort_order, cd.categories_name 第二次查询;此表可能包含一个categories\u id SELECT count(*) AS total FROM products_to_categories

第一次查询;两个表都包含所有
类别\u id
s

SELECT 
* 
FROM 
categories c,
categories_description cd 
WHERE c.categories_id = cd.categories_id 
ORDER BY sort_order, cd.categories_name
第二次查询;此表可能包含一个
categories\u id

SELECT 
count(*) 
AS total 
FROM 
products_to_categories 
WHERE 
categories_id = "'+ catid +'"'
我需要一种方法来对第一个查询中的所有类别进行排序(以生成一个列表),并在一个SQL查询中对第二个查询给出yes/no或0/1

结果如下所示:

categories_id | categories_name | total(*)
    1         |    categorie1   |   21       
    2         |    categorie2   |   0 (if categories_id in  'products_to_categories' does not exist
select 
  c.categories_id,
  cd.categories_name,
  count(p2c.categories_id) as total
from
  categories c
  join categories_description cd
    on c.categories_id = cd.categories_id
  left join product_to_categories p2c
    on p2c.categories_id = c.categories_id
  group by 
    c.categories_id, 
    cd.categories_name
  order by c.sort_order, cd.categories_name
我在以下代码中需要它:

var dbSize = 5 * 1024 * 1024; // 5MB
var db = openDatabase("Oscommerce", "1.0", "Oscommerce Database", dbSize);

var categories={};

var list_str = '';
db.transaction(function (tx) {
    tx.executeSql('SELECT * FROM categories c,categories_description cd WHERE c.categories_id = cd.categories_id ORDER BY categories_id', [], function (tx, results) {
        list_str += '<ul data-role="listview" data-inset="true" data-theme="d">';
        var len = results.rows.length, i;

        for (i = 0; i < len; i++) {
            var r = results.rows.item(i);
            categories[r.categories_id] = r;
        }
        for(key in categories)
        {
            var parent = 0;  
            var value=categories[key];
            catId = value['categories_id'];
            catName = value['categories_name'];
            catImage = value['categories_image'];
            parentId = value['parent_id'];
            if (parentId == parent) 
            {
                list_str += '<li id="'+ catId +'"><a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'"><h3>' + catName + '</h3><p>' + catImage + '</p></a></li>';
                ///i need to do an else around here if the rowed list has products
            }
        }
        list_str += '</ul>';

        $('#parents').html(list_str).find('ul').listview();
    }); 
});
var dbSize=5*1024*1024;//5MB
var db=openDatabase(“Oscommerce”、“1.0”、“Oscommerce数据库”、dbSize);
变量类别={};
变量列表_str='';
数据库事务(功能(tx){
tx.executeSql('从类别c、类别描述cd中选择*,其中c.categories\u id=cd.categories\u id按类别排序,\u id',[],函数(tx,结果){
list_str+='
    ; var len=results.rows.length,i; 对于(i=0;ilist_str+='
  • (观察列表中的计数气泡)。

    尝试以下方法:

    categories_id | categories_name | total(*)
        1         |    categorie1   |   21       
        2         |    categorie2   |   0 (if categories_id in  'products_to_categories' does not exist
    
    select 
      c.categories_id,
      cd.categories_name,
      count(p2c.categories_id) as total
    from
      categories c
      join categories_description cd
        on c.categories_id = cd.categories_id
      left join product_to_categories p2c
        on p2c.categories_id = c.categories_id
      group by 
        c.categories_id, 
        cd.categories_name
      order by c.sort_order, cd.categories_name
    

    尝试以下方法:

    categories_id | categories_name | total(*)
        1         |    categorie1   |   21       
        2         |    categorie2   |   0 (if categories_id in  'products_to_categories' does not exist
    
    select 
      c.categories_id,
      cd.categories_name,
      count(p2c.categories_id) as total
    from
      categories c
      join categories_description cd
        on c.categories_id = cd.categories_id
      left join product_to_categories p2c
        on p2c.categories_id = c.categories_id
      group by 
        c.categories_id, 
        cd.categories_name
      order by c.sort_order, cd.categories_name
    

    此选择应该是您想要的:

    SELECT 
     c.categories_id, cd.categories_name, 
     case when aa.total_per_id is null then 0
       else aa.total_per_id 
     end as total
    FROM categories as c
       join categories_description as cd on c.categories_id = cd.categories_id
       left join (
        select a.categories_id, 
         count(*) as total_per_id from product_to_categories a
        group by a.categories_id ) as aa on aa.categories_id = c.categories_id
    ORDER BY c.sort_order, cd.categories_name;
    

    此选择应该是您想要的:

    SELECT 
     c.categories_id, cd.categories_name, 
     case when aa.total_per_id is null then 0
       else aa.total_per_id 
     end as total
    FROM categories as c
       join categories_description as cd on c.categories_id = cd.categories_id
       left join (
        select a.categories_id, 
         count(*) as total_per_id from product_to_categories a
        group by a.categories_id ) as aa on aa.categories_id = c.categories_id
    ORDER BY c.sort_order, cd.categories_name;
    


    正如我将要写的,除了第一个连接应该是内部连接,而第二个连接需要是左连接,并且您可能希望保留order by子句。@Neil说得好,Neil。我确实忘记了左连接,但我当然希望它出现在那里!更新了我的查询。@Piotr,您的查询只会提取包含内容的类别这是一种产品,不是全部categories@wHiTeHaT请使用最新版本的查询。正如Neil注意到的,我忘记了left join。当前left join已添加,并拉取所有类别。它运行…..编辑:left join products_to_categories to:left join products_to_categories我没有权限执行我将要编写的内容,除了t第一个联接应该是内部联接,而第二个联接需要是左联接,您可能希望保留order by子句。@尼尔,说得好,尼尔。我确实忘记了左联接,但我当然希望它存在!更新了我的查询。@Piotr,您的查询只提取包含产品的类别,而不是所有类别categories@wHiTeHaT请使用最新版本的查询。正如Neil注意到的,我忘记了左连接。当前左连接已添加,并拉取所有类别。它运行…..编辑:左连接产品到类别到:左连接产品到类别我没有权限执行此操作。您现在是否使用上述查询创建了一个虚拟表?@wHiTeHaT不,我不知道。我做了什么s virtual table?@wHiTeHaT查询正在运行。但我发现您已经接受了另一个答案,这个答案也是正确的。谢谢@Panayotis Matsinopoulos,查询对我不起作用,因为您在“产品”到“产品”类别上也犯了错误,应该是“产品”到“产品”类别(我猜piotr拿走了你的一些代码,因为他的第一个部分工作正常)您现在是否使用上述查询创建了虚拟表?@wHiTeHaT不知道。什么是虚拟表?@wHiTeHaT该查询正在运行。但我发现您已接受了另一个答案,该答案也是正确的。谢谢@Panayotis Matsinopoulos,该查询对我不起作用,因为您在产品类别上也犯了错误,应该是产品类别_类别(我猜piotr从你的代码中提取了一些,因为他的第一个代码部分工作)