2011-10-26 60 views
0

第一個查詢;兩個表都包含全部categories_id s計數3個表格並檢查其中一個是否存在ID

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 +'"' 

我需要一種方法來排第一個查詢所有類別(做一個列表),並給我在第二個查詢是/否或0/1一個SQL查詢。

結果是這樣的:

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(觀看列表中的計數氣泡)。

回答

2

這個選擇應該是你想要什麼:

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; 
+0

你現在做了一個虛擬表與上述查詢? – wHiTeHaT

+0

@wHiTeHaT不,我不知道。什麼是*虛擬桌面*? –

+0

查詢不起作用 – wHiTeHaT

1

試試這樣的事情:

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 
+1

正是我所要寫,但第一個加入應該是一個內部聯接,而第二個連接需要是左連接,並且您可能需要保留order by子句。 – Neil

+0

@尼爾好點,尼爾。我確實已經忘記了那次左路加入,但我當然希望它在那裏!更新我的查詢。 –

+0

@Piotr,您的查詢只會提取包含產品的類別,而不是所有類別 – wHiTeHaT

相關問題