简体   繁体   English

带联接和计数的MySQL查询

[英]mySQL Query With Joins and Counts

I have a my sql DB and using php to access it. 我有我的SQL数据库,并使用php访问它。

My tables look like: 我的表格如下:

Flights Table Flight ID, Time, Airport 航班表航班号,时间,机场

Seats Table Flight ID, Seat Number 座位表航班号,座位号

I have the following query: 我有以下查询:

SELECT f.FlightID 'ID', Count(*) 'Seats Booked' FROM flights f, seats s  GROUP BY f.FlightID

I want the out put to be 我希望输出

Flight ID Seats Booked 预订的航班ID座位

ID Num seats book ID Num座位簿

The problem is currently it shows this: 问题是目前它显示此:

[{"ID":"0","Seats Booked":"37"},{"ID":"1234","Seats Booked":"37"}] (The output is JSON encoded)

When it should be be like 10 and 27 if you follow me? 如果跟着我,什么时候应该是10和27? It doesnt seem to be the join correctly. 它似乎不是正确的联接。

Please give an explanation of what im doing wrong rather than just an answer. 请说明我做错了什么,而不仅仅是一个答案。

Thanks! 谢谢!

LmC, LmC,

You did not use the clause to make the join. 您没有使用该子句进行联接。 Thats why you got a sum for each grouped instruction. 这就是为什么您为每个分组指令求和的原因。

The correct instruction using your syntax should be something like: 使用语法的正确指令应类似于:

SELECT f.FlightID 'ID', Count(*) 'Seats Booked' 
FROM flights f, seats s  
where s.FlightId = f.FlightId
GROUP BY f.FlightID

Using inner join syntax should be something like: 使用内部联接语法应类似于:

SELECT f.FlightID 'ID', Count(*) 'Seats Booked' 
FROM flights f
inner join seats s  
on  s.FlightId = f.FlightId
GROUP BY f.FlightID

You can do the something without using the flight table. 您可以执行某些操作而无需使用航班表。 The flight table is not making any difference because you are returning a group of flightid. 航班表没有任何区别,因为您要返回一组Flightid。 And the table seats has flightid. 桌子的座位上有Flightid。 The statement below should be enough. 下面的陈述就足够了。 This is true if you are not concerned about flights without booked seats 如果您不担心没有预订座位的航班,这是正确的

SELECT s.FlightID 'ID', Count(*) 'Seats Booked' 
FROM seats s  
GROUP BY s.FlightID

If you wanna show flights without booked seats, then you should use left join to flights. 如果您想显示没有预订座位的航班,则应使用左联接加入航班。 The statement below will solve this issue. 下面的语句将解决此问题。

SELECT f.FlightID 'ID', Count(*) 'Seats Booked' 
FROM flights f
left join seats s  
on  s.FlightId = f.FlightId
GROUP BY f.FlightID

If you don't need anything more than Flight ID from flights table, why not just doing this: 如果您仅需要排行表中的排期ID,那么为什么不这样做:

SELECT s.FlightID, COUNT(s.id) FROM seats s GROUP BY s.FlightID 选择s.FlightID,COUNT(s.id)个席位s GROUP BY s.FlightID

If you also need to calculate flights with no seats, use this: 如果您还需要计算不带座位的航班,请使用以下方法:

SELECT f.flightID, COUNT(s.id) FROM flights f LEFT JOIN seats s ON s.flightID=f.flightID GROUP BY f.flightID 选择f.flightID,COUNT(s.id)个航班f左联接座位s上s.flightID = f.flightID由f.flightID分组

First of all, you should use the join syntax shown below as your syntax is somewhat outdated. 首先,您应该使用下面显示的联接语法,因为您的语法有些过时了。 Second, be sure to include the where clause 其次,请确保包含where子句

SELECT f.FlightID 'ID', count(*) as "seat count"
INNER JOIN seats s On
f.FlightID = s.FlightID
Where f.FlightID = 'some_id'
GROUP BY f.FlightID

Here the problem with your approach is you are not joining your tables. 这里的方法问题是您没有加入表。 What i mean by joining is "Table1" is related to "Table2" with some relationship. 我的意思是加入“表1”与“表2”有某种关系。 In your case it is flight id. 您的情况是航班ID。 So in the other table (Seats table) you will have seats data but that would be available for any specific flight. 因此,在另一个表(座位表)中,您将拥有座位数据,但该数据可用于任何特定的航班。 Therefore in order to have the matching records you need to join that two tables based on flightid so that you get different number of seats for each flight. 因此,为了获得匹配的记录,您需要基于flightid将这两个表连接起来,以便每个航班获得不同的座位数。

Try this sql. 试试这个sql。

select f.flightid as 'id', count(s.seat_number) as seats_booked
from flight f, seats s
where f.flightid=s.flightid
group by f.flightid 

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

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