[英]mysql get non existing results
使用php和mysql,
我有一個表“ subdomains”,其中包含我的應用程序的子域,具體取決於類別。 子域-ID,CAT_ID,子域
有些類別有子域,有些則沒有。 對於那些沒有子域的用戶,我希望該子域為“ www”。
現在在我的子域表中有一些值,例如:
subdomains : id, cat_id, subdomain
1, 6, "sales"
2, 7, "infos"
現在,我有了一個cat_id數組,例如$ aCatIds = array(1,5,6,8);
在mysql查詢的結尾,我想要這樣的東西:
array(
0 => (cat_id="1", subdomain="www") ,
1 => (cat_id="5", subdomain="www") ,
2 => (cat_id="6", subdomain="sales") ,
3 => (cat_id="8", subdomain="www") ,
)
是否只有一個mysql查詢可能嗎?
我使用php並嘗試了以下操作:
$stmt = "select cat_id, ifnull('www', subdomain) as subdomain
from subdomains
where cat_id in (". implode(',', $aCatIds) .")";
但是我只得到設置的值(在上面的示例中為6),並且我不知道如何告訴mysql獲取期望的數組。
要使此工作有效,您必須(左)加入現有數據(cat_id確實存在的地方)。 例如,如果我假設存在一個cat_id
引用的cat
表,則“ t”將是這樣:
SELECT c.id as cat_id,IFNULL(s.subdomain,'www')
FROM cat c
LEFT JOIN subdomains s
ON s.cat_id = c.id
WHERE c.id IN (1,2,3,4);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.