mardi 29 décembre 2015

SQLITE : inline views or nested subqueries?

I'm trying to write a query on two simple tables. Tables are simple, the query is not :)

Anyway... Here is the database scheme : enter image description here

and here is an overview of table content :

enter image description here

I'm trying to write a query that would list all assets in corresponding table, only if the are marked as "wanted" (meaning the boolean field asset_owned =0) and that are referenced for another owner as "owned".

This is what I have so far and it works :

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)

But, in the same query (if possible) I would like to get the owner name as well.

The first result of such a query on those table would be :

me,003,Test003,you.

I've tried inline SELECT and nested subqueries like :

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)

but of course that does not work.

Thanks for any direction you could point me to.

happy new year

Mathias

Aucun commentaire:

Enregistrer un commentaire