简体   繁体   English

如何查询一个表的同一列中的两个不同对象,这些对象与另一个表中的不同列相关?

[英]How can i make a query of two different objects in the same column of one table, relationed to different columns in another table?

I have the tables我有桌子

people人们

id INTEGER编号 INTEGER

name姓名

phone_number电话号码

PRIMARY KEY(id)主键(id)

phone_calls电话呼叫

id INTEGER编号 INTEGER

caller_number来电者号码

receiver_number接收者号码

year

month

day

duration期间

PRIMARY KEY(id)主键(id)

I want to take the information like我想获取类似的信息

name1 (as caller) name1(作为呼叫者) name2 (as receiver) name2(作为接收者)
nikolas尼古拉斯 mary玛丽
kostas科斯塔斯 bob鲍勃

i managed with SELECT name, caller_number FROM people JOIN phone_calls ON people.phone_number = phone_calls.caller_number WHERE year = AND month = AND day = AND duration =;我用 SELECT 名称管理,caller_number FROM people JOIN phone_calls ON people.phone_number = phone_calls.caller_number WHERE year = AND month = AND day = AND duration =; to get要得到

name as caller作为呼叫者的名字 caller_number来电者号码
nikolas尼古拉斯 6999999999 6999999999
kostas科斯塔斯 688888888 688888888

or with SELECT name, receiver_number FROM people JOIN phone_calls ON people.phone_number = phone_calls.receiver_number WHERE year = AND month = AND day = AND duration =;或使用 SELECT 名称,receiver_number FROM people JOIN phone_calls ON people.phone_number = phone_calls.receiver_number WHERE year = AND month = AND day = AND duration =; to get要得到

name as receiver名称为接收者 receiver_number接收者号码
mary玛丽 6777777777 6777777777
bob鲍勃 6555555555 6555555555

or with SELECT caller,receiver_number FROM phone_calls WHERE year = AND month = AND day = AND duration;或与 SELECT 呼叫者,receiver_number FROM phone_calls WHERE year = AND month = AND day = AND duration; to get要得到

caller_number来电者号码 receiver_number接收者号码
6999999999 6999999999 6777777777 6777777777
688888888 688888888 6555555555 6555555555

or with SELECT name, receiver_number FROM people JOIN phone_calls ON people.phone_number = phone_calls.caller_number WHERE year = AND month = AND day = AND duration =;或使用 SELECT 名称,receiver_number FROM people JOIN phone_calls ON people.phone_number = phone_calls.caller_number WHERE year = AND month = AND day = AND duration =; to get要得到

name as caller作为呼叫者的名字 receiver_number接收者号码
nicolas尼古拉斯 6777777777 6777777777
kostas科斯塔斯 6555555555 6555555555

so i get the name at the telephone number of the caller or the name and the telephone number of the receiver, but not name of the caller and name of the receiver in the same place.所以我在呼叫者的电话号码或接收者的姓名和电话号码上得到姓名,但在同一个地方没有呼叫者的姓名和接收者的姓名。

You need 2 joins to get both caller and receiver's name.您需要 2 个连接才能同时获取呼叫者和接收者的姓名。 For example例如

SELECT caller.name, receiver.name
FROM phone_calls
JOIN people caller ON caller.phone_number = phone_calls.caller_number
JOIN people receiver ON receiver.phone_number = phone_calls.receiver_number
WHERE <your conditions>

暂无
暂无

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

相关问题 如何使来自同一表的两个$ db-&gt; query具有按列彼此独立地位于一个php页面中的两个不同ORDER? - How to make two $db->query from the same table with two different ORDER by column that each other stand independenty in one php page? 如何访问mysql中另一个表的两个不同列对应的表的一列? - How to access one column of a table corresponding to two different columns of another table in mysql? 将一个表中的两列连接到其他表中的一列,则两列中的每一个都返回不同或相同的结果 - Joining two columns from one table to one column in other returning different or the same result for each of the 2 columns 我如何加入两个不同的查询语句,其中包含不同的表,列名 - how can i join the two different query statement,which contain different table ,column name 如何在不同的表列中获取另一个表数据? - How can i fetch another table data in different table column? MySQL将两个表列复制到具有不同列名的另一个表 - MySQL copy two table columns to another table with different column names 如何从一个mysql表复制到具有不同列类型定义的另一张表? - How can I copy from one mysql table to another table with different column type definitions? 为另一个表中的 2 个不同列检索表的同一列 - Retrieve same column of a table for 2 different columns in another table MySQL-连接两个表,表1中不同的列在表2中的同一列上 - MySQL - Join two tables, different columns in table 1 on the same column in table 2 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM