SQLITE:内联视图或嵌套子查询?

SQLITE : inline views or nested subqueries?

我正在尝试针对两个简单的 table 编写查询。表很简单,查询不是:)

总之... 这是数据库方案:

这里是 table 内容的概述:

我正在尝试编写一个查询,列出相应 table 中的所有资产,前提是标记为 "wanted"(表示布尔字段 asset_owned =0)并且被另一个所有者引用为 "owned".

这是我目前所拥有的并且有效:

SELECT
    user.user_pseudo AS REQUESTER,
    asset.asset_sku AS SKU,
    asset.asset_name AS ASSET_NAME
FROM
    asset
INNER JOIN user ON asset.id_user = user.id
WHERE
    asset.asset_owned = 0
AND 
    asset.asset_sku IN (SELECT asset.asset_sku FROM asset   WHERE asset.asset_owned = 1)

但是,在同一个查询中(如果可能的话)我也想获得所有者姓名。

对那些 table 进行此类查询的第一个结果将是:

我,003,Test003,你。

我试过内联 SELECT 和嵌套子查询,例如 :

SELECT
user.user_pseudo as ASKER,
asset.asset_sku as SKU,
asset.asset_name as NAME,
subquery1.user.user_pseudo as OWNER
FROM
asset
INNER JOIN user ON asset.id_user = user.id,
(SELECT user.user_pseudo.asset_asset_sku FROM asset INNER JOIN user ON asset.id_user = user.id WHERE asset.asset_owned = 1) subquery1
WHERE
asset.asset_owned = 0 AND
subquery1.asset.asset_sku IN (SELECT asset.asset_sku FROM asset INNER JOIN user ON asset.id_user = user.id WHERE asset.asset_owned=1)

但这当然行不通。

感谢您给我指出的任何方向。

新年快乐

马蒂亚斯

所以这对我来说很有趣(我正在学习 SQL,所以这是很好的练习!)- 我很欣赏这个非常明确的问题。

希望这对您有用 - 我使用了两个 sub-queries('owner' 和 'requester' 各一个)然后加入了 SKU 和名称。它在 SQLite 中使用上面显示的小样本数据工作。

SELECT requester, subq1.SKU, subq1.name, owner
FROM 
   (SELECT pseudo AS requester, SKU, name
    FROM asset, user
    WHERE owned = 0
    AND user.id = id_user) subq1,
   (SELECT pseudo AS owner, SKU, name
    FROM asset, user
    WHERE owned = 1
    AND asset.id_user = user.id) subq2
WHERE subq1.SKU = subq2.SKU
AND subq1.name = subq2.name;