0

我正在开发一个 phonegap 应用程序——找出谁欠谁一杯酒。我想我已经接近解决方案,但如果没有外部连接(SQL Lite 不支持),就无法计算出最后一部分。

该数据库由代表购买者和接收者的用户 ID 以及单独的饮料类型(啤酒、葡萄酒、鸡尾酒等)构成。我想检查并取消相同类型的饮料,以计算出每种类型的饮料谁领先。

结果应该是一个表格,每个饮料代码都在旁边,欠的数字(可能是负数)

var sql = "select too.drinkType, ifnull(fro.cnt, 0) - ifnull(too.cnt, 0) as cnt FROM "+
"(SELECT drinkType, count(drinkType) as Cnt FROM drink "+
"WHERE boughtBy = "+0+" and boughtFor = " + id + " GROUP BY drinkType) too "+
" LEFT OUTER JOIN  " +
"(SELECT drinkType, count(drinkType) as Cnt FROM drink "+
"WHERE boughtBy = "+id+" and boughtFor = " + 0 + " GROUP BY drinkType) fro "+
"on too.drinkType = fro.drinkType "

tx.executeSql(sql, [], getDrinksNet_success);

这几乎可以工作,但错过了仅由第二个查询(来回)返回的drinkTypes。我认为外部连接是我需要的,但 phonegap 似乎不支持这一点。有办法解决吗?

4

2 回答 2

0

尝试使用这样的查询,根据谁买了饮料来计数:

SELECT drinkType, SUM(CASE WHEN boughtBy = :id THEN 1 ELSE -1 END) AS cnt
FROM drink
WHERE (boughtBy = :id AND boughtFor = 0)   
OR (boughtFor = :id AND boughtBy = 0)
GROUP BY drinkType
于 2013-04-14T10:06:33.537 回答
0

在一般情况下,您可以FULL OUTER JOIN通过组合左外连接和右外连接来实现 a,并RIGHT OUTER JOIN通过使用 aLEFT OUTER JOIN并交换两个表来实现 a。您的查询将如下所示:

SELECT ... FROM
(SELECT ...) too
LEFT OUTER JOIN
(SELECT ...) fro
ON too.drinkType = fro.drinkType
UNION
SELECT ... FROM
(SELECT ...) fro
LEFT OUTER JOIN
(SELECT ...) too
ON fro.drinkType = too.drinkType
于 2013-04-14T10:57:50.117 回答