我有2个问题:
select COUNT(o.id_offer) from offers o, product p where
p.id_product = "+ productID +" and o.id_offer = "+ offerID +" and (b.type = 0 or o.type = "A")
此查询返回某个产品的计数
select p.id_product, p.name, s.id_supplier, s.name from product p, suppliers s where p.id_supplier = s.id_supplier
此查询返回所有产品数据
我想将两个查询合并为一个,如下所示:
select (select COUNT(o.id_offer) from offers o, product p where
p.id_product = p.id_product and o.id_offer = 13345 and (b.type = 0 or o.type = "A")) count,p.id_product, p.name, s.id_supplier, s.name from product p, suppliers s where p.id_supplier = s.id_supplier
任何人都知道如何在informix中执行此操作?
答案 0 :(得分:1)
请使用显式连接表示法,而不是FROM子句表示法中表名的逗号列表。
第一个查询需要进行概括以生成产品ID和匹配计数。
SELECT p.id_product, COUNT(o.id_offer) AS offer_count
FROM offers o
JOIN product p ON p.id_product = o.id_product -- Guessed column
WHERE (p.type = 0 OR o.type = 'A')
GROUP BY p.id_product
第二个查询可以转换为:
SELECT p.id_product, p.name, s.id_supplier, s.name
FROM product p
JOIN suppliers s ON p.id_supplier = s.id_supplier
这两个查询也可以与连接组合使用:
SELECT x.id_product, x.product_name, x.id_supplier, x.supplier_name, y.offer_count
FROM (SELECT p.id_product, p.name, s.id_supplier, s.name
FROM product p
JOIN suppliers s ON p.id_supplier = s.id_supplier
) AS x
JOIN (SELECT p.id_product, COUNT(o.id_offer) AS offer_count
FROM offers o
JOIN product p ON p.id_product = o.id_product -- Guessed column
WHERE (p.type = 0 OR o.type = 'A')
GROUP BY p.id_product
) AS y
ON x.id_product = y.id_product