复杂搜索SQL查询

时间:2013-03-26 18:54:44

标签: mysql select

我需要你的sql查询帮助。

我需要一个表中的选择值,其中mathing元素具有最小的顺序,目前我有以下查询:

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

当最小的“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)

我用“min(a.ord)”替换“AND a.ord = 1”但没有成功..

我怎么能这样做,不做两个单独的查询。

有没有办法可以添加某种

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

在当前的SQL中?

谢谢你!

5 个答案:

答案 0 :(得分:1)

您可以通过计算每个表的最小点数然后在联接中使用它来实现:

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

答案 1 :(得分: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

希望我的问题正确

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分:0)

更新:

我已经用嵌套的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在一个查询中使用多项选择的见解!