0

我需要你的 sql 查询帮助。

我需要在数学元素具有最小顺序的表中选择值,目前我有以下查询:

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)

我已将“AND a.ord =1”替换为“min(a.ord)”,但没有成功。

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

有没有办法我可以添加一些东西

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

在当前的 SQL 里面?

谢谢你!

4

5 回答 5

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
于 2013-03-26T19:20:40.867 回答
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
于 2013-03-26T19:22:56.590 回答
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 对在一个查询中使用多项选择的见解!

于 2013-03-27T00:57:04.503 回答
0

像这样的东西可能是你正在寻找的东西。您可以更改ORDER BY c.ord ASC, a.ord ASCORDER 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
于 2013-03-26T19:24:48.483 回答
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

希望我能解决你的问题

于 2013-03-26T19:17:06.417 回答