如何选择要在一行中显示的字段数组或在一行中显示json

时间:2015-07-21 10:24:04

标签: php mysql

select 
       s.id,
       s.name,
       c.PNAME city,
       s.user_name,
       s.logo,
       sqrt(pow((latitude-'1'),2)+pow((longitude-'1'),2)) as distance ,
       (SELECT 
              ph.phone_number 
        FROM `phones` as ph 
        WHERE (ph.type='tel' AND ph.`shop_id`=s.id)) as phons
from shops as s
JOIN addresses  as a ON s.address_id=a.id 
JOIN city as c ON c.id=a.city
where 1
order by('distance ASC')
limit 0,20 ;

以上code给我一个error

  

错误代码1242,SQL状态21000:子查询返回多于1行

2 个答案:

答案 0 :(得分:2)

    select s.id,s.name,
c.PNAME city,s.user_name,s.logo,
sqrt(pow((latitude-'1'),2)+pow((longitude-'1'),2)) as distance ,
((SELECT GROUP_CONCAT(ph.phone_number) FROM `phones` as ph WHERE (ph.type='tel'))) as phons
from shops as s
 JOIN addresses  as a ON s.address_id=a.id 
JOIN city as c ON c.id=a.city
where 1
order by('distance ASC')
limit 0,20 ;

GROUP_CONCAT reult numberphone in row 

答案 1 :(得分:1)

等子查询中使用limit 1
SELECT ph.phone_number FROM `phones` as ph 
        WHERE (ph.type='tel' AND ph.`shop_id`=s.id limit 1