2013-03-26 100 views
0

我需要你的幫助與SQL查詢。複雜的搜索SQL查詢

我需要在該mathing元素具有smalest順序表中選擇值,目前我有以下查詢:

SELECT p.confirmation, 
    p.lname, 
    p.fname, 
    p.profiletype, 
    a.address1, 
    a.city, 
    c.contactinfo 
FROM profile p 
LEFT JOIN contact c 
    ON p.confirmation = c.profileid 
    AND c.ord = 1 
LEFT JOIN address a 
    ON p.confirmation = a.profileid 
    AND a.ord =1 

這項工作做得很好,當smalest「a.ord」或c.ord 「是1,但最小值不會永遠是一個,也可能是其他任何數字,所以我試着不更迭如下:

SELECT p.confirmation, 
    p.lname, 
    p.fname, 
    p.profiletype, 
    a.address1, 
    a.city, 
    c.contactinfo 
FROM profile p 
LEFT JOIN contact c 
    ON p.confirmation = c.profileid 
    min(c.ord) 
LEFT JOIN address a 
    ON p.confirmation = a.profileid 
    min(a.ord) 

我已經取代」 AND a.ord = 1 「by」min(a.ord)「沒有成功。

我怎麼能做到這一點,沒有做出兩個單獨的查詢。

有沒有一種方法,我可以在排序的

SELECT c.ord FROM contact c WHERE p.confirmation = c.profileid ORDER BY c.ord LIMIT 1 

當前SQL裏面添加的東西?

謝謝!

回答

0
AND a.ord =1 // if a.cord is always 1 
    AND a.ord <=1 // if a.cord is always <1 (eg. .9 or .75) 
    // or just leave it.. i dont know your script, but is it needed? 
    // same at c.cord! 
    // but rather use where! see below 

而且作爲一個例子:

SELECT 
    p.confirmation, 
    p.lname, 
    p.fname, 
    p.profiletype, 
    a.address1, 
    a.city, 
    c.contactinfo 
FROM 
    profile AS p 
LEFT JOIN 
    contact AS c 
ON 
    p.confirmation=c.profileid 
LEFT JOIN 
    address AS a 
ON 
    p.confirmation=a.profileid 
WHERE 
    c.ord=1 AND a.ord=1 
ORDER BY 
    c.ord ASC, a.ord ASC 

希望我得到了你的問題所在

1

您可以通過計算分ORD爲每個表,然後使用,在加入做:

SELECT p.confirmation, p.lname, p.fname, p.profiletype, 
     a.address1, a.city, c.contactinfo 
FROM profile p LEFT JOIN 
    (select c.profileid, MIN(c.ord) as minord 
     from contact c 
     group by c.profileid 
    ) minco 
    on minco.profileid = c.profileid left join 
    contact c 
    ON p.confirmation = c.profileid and minco.minord = c.ord LEFT JOIN 
    (select a.profileid, min(a.ord) as minord 
     from address a 
     group by a.profileid 
    ) minao 
    on minao.profileid = c.profileid left join 
    address a 
    ON p.confirmation = a.profileid and minao.minord = a.ord 
+0

close ...我有多個聯繫人爲每個配置文件,並按「ord」排序。我需要每個配置文件的第一個聯繫人顯示在我的搜索中。類似於:AND c.ord =(SELECT ord FROM contact where WHERE profileid = p.confirmation ORDER BY ord LIMIT 1)。但我現在知道,這是不正確的SQL! – 2013-03-26 19:25:47

+0

@DanielBerthiaume。 。 。這就是這個查詢正在做的事情。它只是將ord的最小值計算爲單獨的子查詢。 – 2013-03-26 19:27:48

+0

謝謝你,答案並沒有解決我的問題,但它確實幫助我找到了我自己的答案,並且對嵌套select有所瞭解。 – 2013-03-26 19:47:34

0

只需在MySQL中使用LIMIT 1即可在訂購後獲得第一行:

SELECT p.confirmation, 
    p.lname, 
    p.fname, 
    p.profiletype, 
    a.address1, 
    a.city, 
    c.contactinfo 
FROM profile p 
LEFT JOIN contact c 
    ON p.confirmation = c.profileid 
LEFT JOIN address a 
    ON p.confirmation = a.profileid 
ORDER BY c.ord, a.ord 
LIMIT 1 
0

這樣的事情可能就是你要找的東西。您可以根據所需的優先級將ORDER BY c.ord ASC, a.ord ASC更改爲ORDER BY a.ord ASC, c.ord ASC

SELECT p.confirmation, 
    p.lname, 
    p.fname, 
    p.profiletype, 
    a.address1, 
    a.city, 
    c.contactinfo 
FROM profile p 
LEFT JOIN contact c 
    ON p.confirmation = c.profileid 
LEFT JOIN address a 
    ON p.confirmation = a.profileid 
ORDER BY c.ord ASC, a.ord ASC 
LIMIT 1 
0

更新:

我finalyl解決我的問題與嵌套SELECT:

SELECT p.confirmation, 
     p.lname, 
     p.fname, 
     p.profiletype, 
     a.address1, 
     a.city, 
     c.contactinfo 
    FROM profile p 
    LEFT JOIN contact c 
     ON p.confirmation = c.profileid 
     AND c.ord=(SELECT ord FROM contact WHERE profileid= p.confirmation ORDER BY ord LIMIT 1) 
    LEFT JOIN address a 
     ON p.confirmation = a.profileid 
     AND a.ord=(SELECT ord FROM address WHERE profileid= p.confirmation ORDER BY ord LIMIT 1) 

謝謝#Gordon Linoff您的洞察力上使用一個查詢的多個選擇!