简体   繁体   English

SQL中的额外列选择查询或视图

[英]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. 因此,我目前在数据库中有3张表,一张包含有关会员的信息,一张包含有关其船长(长度)的信息,一张包含价格的信息。

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) Naam应该盖的地方Naam(从第一张表开始)
  • Where Adres should be Lid.Adres (from the 1st table) 应该在哪里盖上Adres.Adres(来自第一张表)
  • Where Email should be Lid.Email (from the 1st table) 电子邮件应为Lid.Email的位置(来自第一个表)
  • Where contributie should be Tarief.Bedrag where Soort = 'contributie' 其中贡献应该是Tarief.Bedrag,其中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 liggeld应该在哪里(Tarief.Bedrag其中Soort ='liggeld')x(Schip.lengte),并且如果Lid.Schip为空,则此数量应为0
  • Where totaal should be liggeld + contributie 应当在哪里进行小费+贡献
  • At last, everyone who has lid.ContributieBetaald = 'ja' should not be in the list 最后,每个有lid.ContributieBetaald ='ja'的人都不应该在列表中

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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