简体   繁体   English

SQL查询2个不同的表

[英]SQL query for 2 different tables

I have tables: 我有桌子:

  1. Trips: Trip_Num, Trip_Type, etc. 行程:Trip_Num,Trip_Type等。
  2. User's Trips History: Username, Trip_Num, etc. 用户的行程历史记录:用户名,Trip_Num等

I am doing a project in php and need to do a SQL query for a specific user (by his username) - need to know the Trip_Type for the Trip_Num in the User's Trips History table. 我正在用php做一个项目,需要针对特定​​用户(通过其用户名)执行SQL查询-需要在“用户的旅行历史记录”表中了解Trip_Num的Trip_Type。

Attached the tables I have: 附上我有的表:

Trips 旅行

在此处输入图片说明

User's Trips History 用户的旅行记录

在此处输入图片说明

Update: I need to do count on each value in the Trip_type. 更新:我需要依靠Trip_type中的每个值。 I mean I want to see "on foot" - 4 occurences, "Bicycle" - 3 occurences, this is good?...........................................................................‌​ select Trips_History.*,Trips.Trip_Type, COUNT(Trip_Type) AS Trip_Type_occurrence FROM Trips History INNER JOIN Trips on Trips_History.Trip_Num = Trips.Trip_Num GROUP BY Trip_Type ORDER BY Trip_Type_occurrence DESC – 我的意思是我想看“徒步”-4次,“自行车”-3次,这很好吗? .................................................. select选择Trips_History。*,Trips.Trip_Type,COUNT(Trip_Type)AS Trip_Type_occurrence来自行程历史记录INNER JOIN Trips_History.Trip_Num = Trips.Trip_Num GROUP BY Trip_Type ORDER BY Trip_Type_occurrence DESC –

A simple JOIN will do: 一个简单的JOIN可以做到:

SELECT
    th.*,
    t.Trip_Type
FROM Trip_History AS th
INNER JOIN Trips AS t
    ON t.Trip_Num = th.Trip_Num

尝试这个:

 select Trips_History.*,COUNT(Trips.Trip_Type) AS Trip_Type_occurrence FROM Trips_History INNER JOIN Trips on Trips_History.Trip_Num = Trips.Trip_Num  GROUP BY Trip_Type ORDER BY Trip_Type_occurrence DESC

Try this 尝试这个

select t2.traveller_username,t1.Trip_Type from 
trips as t1 inner join trips_history as t2 on t1.Trip_num=t2.trip_num

Try, 尝试,

SELECT t.Trip_Type, u.Trip_Num from trips at t 
JOIN user_trips_history ON u.Trip_Num = t.Trip_Num 
WHERE u.Traveller_Username = 'username';

Try doing: 尝试做:

SELECT t.Trip_Type, u.Trip_Num FROM trips as t 
JOIN user_trips_history as u ON t.Trip_Num=u.Trip_Num 
WHERE u.Traveller_Username = 'USERNAME';

you can try this - 你可以试试这个-

SELECT users_trip_history.trip_num, trips.trip_type, COUNT(trips.trip_type) AS trip_occurences
FROM users_trip_history
INNER JOIN trips
ON users_trip_history.trip_num = trips.trip_num 
WHERE users_trip_history.traveler_username = "name"
GRUOP BY trip_num;

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

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