ยังไงที่จะกลับมากกว่าหนึ่งแถวออกจาก subquery ใช้เป็นสำนวน

0

คำถาม

select case 
         when p.property_type ='APARTMENT_COMMUNITY' 
           then (select fp.bedroom_count 
                 from floor_plans fp 
                 where fp.removed = false 
                 and fp.property_id=p.id) 
         else (select pu.bedroom_count 
               from property_units pu 
               where pu.removed = false 
               and pu.property_id=p.id) 
        end 
from properties p 
where p.id =550

ฉันมีเรื่องนี้ bedroom_count ไม่ใช่คนเดียวแถวนั้นมันทำให้เกิดข้อผิดพลาดนี้

เกิดข้อผิดพลาด:มากกว่าหนึ่งแถวกลับมาโดย subquery ใช้ในการแสดง

ฉันต้องได้ผลในกรณีนี้มันมีอีกทางออกสำหรับเรื่องนี้?

postgresql sql
2021-11-24 06:24:39
3
0

ข้อผิดพลาดงออกมาจากความจริงที่เหมือนกันก่อนหรือสอง subquery อบแทนมากกว่า 1 แถวสำหรับให้ property_id(550). จากความคิดเห็นของคุณ

ฉันอยากให้พวกเขาทั้งหมเป็นผลลัพธ์

ผมเดาว่านั่นคือสิ่งที่คุณต้องการคือทิ้งร่วมกับทั้งสองตารางข้อมูลเรียบร้อยแล้ว. ลองนี่

select p.property_type, coalesce(fp.bedroom_count, pu.bedroom_count) as bedroom_count
  from properties p
  left join floor_plans fp 
    on p.property_type = 'APARTMENT_COMMUNITY' and fp.removed = false and fp.property_id = p.id
  left join property_units pu
    on p.property_type <> 'APARTMENT_COMMUNITY' and pu.removed = false and pu.property_id = p.id
 where p.id = 550
2021-11-24 06:50:23
0

มันฟังดูเหมือนคุณจริงๆต้องการจะร่วมมือกับตารางข้อมูลเรียบร้อยแล้ว. อย่างที่คุณต้องการห้องนอนของนับออกมาจากคนโต๊ะหรืออีกคน,ถึงแม้ว่าคุณจะต้องริงๆข้างนอร่วมโต๊ะมันทำความสะอาด.

select p.*, coalesce(fp.bedroom_count, pu.bedroom_count) as bedroom_count
from properties p
left join floor_plans fp on p.property_type = 'APARTMENT_COMMUNITY' 
                         and fp.property_id = p.id
                         and fp.removed = false 
left join property_units pu on p.property_type <> 'APARTMENT_COMMUNITY' 
                            and pu.property_id = p.id
                            and pu.removed = false 
where p.id = 550
order by p.id;

หรือใช้ UNION ALL:

select p.*, fp.bedroom_count
from properties p
join floor_plans fp on fp.property_id = p.id and fp.removed = false 
where p.id = 550
and p.property_type = 'APARTMENT_COMMUNITY'
union all
select p.*, pu.bedroom_count
from properties p
join property_units pu on pu.property_id = p.id and pu.removed = false 
where p.id = 550
and p.property_type <> 'APARTMENT_COMMUNITY'
order by p.id;

(ถ้า property_type สามารถเป็นโพรโทคอล aimcomment พวกนี้ค้นข้อมูลที่จะต้องการการปรับแก้ค่าการจัดการกับเรื่องนี้)

2021-11-24 06:51:04
0
select  case 
            when p.property_type ='APARTMENT_COMMUNITY' 
                then (  
                    select  array_agg(distinct fp.bedroom_count) 
                    from    floor_plans fp 
                    where   fp.removed = false 
                    and     fp.property_id=p.id ) 
            else (
                    select  (array_agg(distinct pu.bedroom_count)) 
                    from    property_units pu 
                    where   pu.removed = false 
                    and pu.property_id=p.id ) 
        end 
from    properties p 
where   p.id =550

นี่คือคำตอบของปัญหาของฉันเผื่อใครบางคนต้องการมัน

2021-11-24 07:43:36

โอเคดังนั้นมันแน่อยู่แล้วเข้าชุดเหมาะเหม็งเป็นทั้งเธรดคุณกำลังมองหา ครั้งหน้าถ้าคุณถามคำถามได้โปรดแสดงตัวอย่างข้อมูลของและคาดหวังผลให้เราเข้าใจว่าคุณเป็นอะไรขอร้อง
Thorsten Kettner

ใช่ขอโทษนะนี่เป็นครั้งแรกของฉัน))))ขอบคุณมากฉันจะ
Grigor Martiros

ฉันเรียนรู้อะไรได้เยอะนะจากของคุณคำตอบยังไง
Grigor Martiros

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่