【问题标题】:Count 3 tables and check in one of them for existing ID计算 3 个表并检查其中一个表的现有 ID
【发布时间】:2011-10-26 20:15:04
【问题描述】:

第一个查询;两个表都包含所有categories_ids

SELECT 
* 
FROM 
categories c,
categories_description cd 
WHERE c.categories_id = cd.categories_id 
ORDER BY sort_order, cd.categories_name

第二次查询;这个表也许有一个categories_id

SELECT 
count(*) 
AS total 
FROM 
products_to_categories 
WHERE 
categories_id = "'+ catid +'"'

我需要一种方法来对第一个查询中的所有类别进行排列(以制作一个列表),并在一个 SQL 查询中从第二个查询中给我一个是/否或 0/1。

结果如下所示:

categories_id | categories_name | total(*)
    1         |    categorie1   |   21       
    2         |    categorie2   |   0 (if categories_id in  'products_to_categories' does not exist

我在下面的代码中需要它:

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();
    }); 
});

总输出应该会产生一些like this(观察列表中的计数气泡)。

【问题讨论】:

    标签: mysql join count


    【解决方案1】:

    这个选择应该是你想要的:

    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;
    

    【讨论】:

    • 你现在用上面的查询做了一个虚拟表吗?
    • @wHiTeHaT 不,我不知道。什么是虚拟表
    • @wHiTeHaT 查询正在运行。但我看到你已经接受了另一个也是正确的答案。
    • 谢谢@Panayotis Matsinopoulos,该查询对我不起作用,因为您也对 product_to_categories 犯了错误,应该是 products_to_categories(我假设 piotr 使用了您的代码,因为他的第一个代码部分工作)
    【解决方案2】:

    试试这样的:

    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
    

    【讨论】:

    • 正是我要写的,除了第一个连接应该是内连接,而第二个连接需要是左连接,并且您可能希望保留 order by 子句。跨度>
    • @Neil 好点,尼尔。我确实忘记了那个左连接,但我当然打算让它在那里!更新了我的查询。
    • @Piotr ,您的查询仅提取包含产品的类别,而不是所有类别
    • @wHiTeHaT 请使用最新版本的查询。正如尼尔注意到的那样,我忘记了左连接。目前左连接已添加,它会拉出所有类别。
    • 它运行.....编辑:左加入 product_to_categories 到:左加入 products_to_categories 我没有权限这样做
    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多