0

我想加入两个选择查询,但不知道该怎么做。

第一个查询为我提供了有关发票编号、客户、eventid 和金额的信息,这些信息都保存在发票表中:

SELECT invoice.Eventid, invoice.Invoiceno, 
event.clientid, client.clientid, clientname, gross_amount, VAT, total, due
FROM client, invoice, event, 
WHERE event.eventid=invoice.eventid and event.clientid = client.clientid group by invoice.eventid

第二个查询是检查员工的薪水

SELECT event_ma.eventid, salary.staffid, SUM( cost_hour * TIME_TO_SEC( TIMEDIFF( hours, pause ) ) ) /3600 AS costs
FROM salary
JOIN event_ma ON salary.staffid = event_ma.staffid
GROUP BY event_ma.eventid

最后,我想查看每个事件的所有相关成本和收入。

4

3 回答 3

0

您实际上可以使用这种方式:

CREATE VIEW `inv` AS
    SELECT invoice.Eventid AS inve, invoice.Invoiceno, 
    event.clientid, client.clientid, clientname, gross_amount, VAT, total, due
    FROM client, invoice, event, 
    WHERE event.eventid=invoice.eventid and event.clientid = client.clientid group by invoice.eventid

CREATE VIEW `ev` AS
    SELECT event_ma.eventid AS evee, salary.staffid, SUM( cost_hour * TIME_TO_SEC( TIMEDIFF( hours, pause ) ) ) /3600 AS costs
    FROM salary
    JOIN event_ma ON salary.staffid = event_ma.staffid
    GROUP BY event_ma.eventid

您的最终查询为:

SELECT * FROM `inv`, `ev`
    WHERE `inve` = `evee`;

或者你也可以考虑JOIN这样使用 MySQL:

SELECT * FROM `inv`
    JOIN `ev` ON `inv`.`inve` = `ev`.`evee`;
于 2013-04-27T12:18:45.223 回答
0

我理解这个问题的方式,你可以采取第一个查询,然后在eventID匹配的地方加入第二个查询。此外,只是指出您在第一个查询中使用旧的连接语法 - 而不是使用:

Select table1.col, table2.col From
table1, table2
...

你应该使用:

Select table1.col, table2.col From
table1 
INNER JOIN table2 On
table1.colID = table2.colID

...

并回答您的问题:

SELECT invoice.eventid, 
       invoice.invoiceno, 
       event.clientid, 
       client.clientid, 
       clientname, 
       gross_amount, 
       vat, 
       total, 
       due 
FROM   client, 
       invoice, 
       event, 
       inner 
       JOIN (SELECT event_ma.eventid, 
                    salary.staffid, 
                    Sum(cost_hour * Time_to_sec(Timediff(hours, pause))) / 3600 
                    AS 
                          costs 
             FROM   salary 
                    JOIN event_ma 
                      ON salary.staffid = event_ma.staffid 
             GROUP  BY event_ma.eventid) x 
         ON invoice.eventid = x.eventid 
WHERE  event.eventid = invoice.eventid 
       AND event.clientid = client.clientid 
GROUP  BY invoice.eventid 
于 2013-04-27T12:21:05.400 回答
0

您可以编写一个将子查询作为数据源的查询:

select ...
from 
    (select ...) as q1
    inner join (select ...) as q2 on ...
...
于 2013-04-27T12:44:29.543 回答