简体   繁体   中英

Extra columns in sql select query or view

So i currently got 3 tables in my database, One containing information about members, one about their boats (length) and one about prices.

Lid (Member)

会员表

Schip (Boat)

船用桌

Tarief (Prices)

价格

I need to create a query that combines parts of these tables, but i have not found any way how to do this. This is what my 'new' table / select statement result should look like: 在此处输入图片说明

  • Where Naam should be Lid.Naam (from the 1st table)
  • Where Adres should be Lid.Adres (from the 1st table)
  • Where Email should be Lid.Email (from the 1st table)
  • Where contributie should be Tarief.Bedrag where Soort = 'contributie'
  • Where liggeld should be (Tarief.Bedrag where Soort = 'liggeld') x (Schip.lengte) and if Lid.Schip is empty, this amount should be 0
  • Where totaal should be liggeld + contributie
  • At last, everyone who has lid.ContributieBetaald = 'ja' should not be in the list

I hademade an example for how it should look like if the query works completely: 最终应该是什么样子

If anyone knows how to create a query for this or a view and would like to help me, thanks!

Try this:

SELECT l.Naam, l.Adres, l.Email, 
(SELECT Bedrag FROM Tarief WHERE Soort = 'contributie' LIMIT 1) AS 'Contributie',
(SELECT Bedrag FROM Tarief WHERE Soort = 'liggeld' LIMIT 1)*IFNULL(s.length,0) AS 'Liggeld',
(SELECT Bedrag FROM Tarief WHERE Soort = 'contributie' LIMIT 1)+
(SELECT Bedrag FROM Tarief WHERE Soort = 'liggeld' LIMIT 1)*IFNULL(s.length,0)  AS 'Totaal'
FROM Lid l
LEFT JOIN Schip s ON l.Schip=s.Naam
WHERE l.ContributieBetaald <> 'ja';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM