简体   繁体   English

PHP MySQL根据另一张表中的ID在一行中查询两个用户名

[英]Php mysql Query two usernames in one row based on id from another table

I am somewhat new to coding and have been trying to write what I thought would be a straightforward sql query. 我对编码有点陌生,一直在尝试编写我认为会很简单的sql查询的内容。 Please help :) 请帮忙 :)

Table 1: Users
id = user idt
username = user name 

Table 2: Orders
orderid = order id
order_to = user id of person buying 
order_from = user id of person selling
oder_details = text

Basically I want to: 基本上我想:

"Select Username(from), Username(to), order_details FROM mytables WHERE Order id = 1;" 

And get the result as 1 row, I'm not sure how to proceed. 并将结果作为1行,我不确定如何继续。 I thought I could do this with concatenation or something... Can anyone help? 我以为可以通过串联或其他方式执行此操作...有人可以帮忙吗?

You need to use JOIN to link the tables together. 您需要使用JOIN将表链接在一起。

SELECT fu.username AS fromUser, tu.username AS toUser, o.order_details
FROM Orders o
INNER JOIN Users fu
ON fu.id = o.order_to
INNER JOIN Users tu
ON tu.id = o.order_from
WHERE o.orderid = '1';

Because you have two different users that you need the username from, you need to JOIN the Users table twice to get both user's usernames. 因为您有两个不同的用户需要用户名,所以您需要两次JOIN Users表以获取两个用户的用户名。 Each table needs to have it's own alias fu and tu to allow MySQL to differentiate between them. 每个表都必须具有自己的别名futu ,以便MySQL区分它们。 The same goes for the column names in your SELECT statement so that when you fetch the results with PHP, php can differentiate between the two usernames. SELECT语句中的列名也是如此,因此,当您使用PHP获取结果时,php可以区分两个用户名。

You are looking for a JOIN . 您正在寻找JOIN This can be done with a keyword or through WHERE clauses. 可以使用关键字或通过WHERE子句来完成。 For example, 例如,

SELECT * FROM Orders JOIN Users ON Orders.order_to=Users.id

The documentation can be found here: http://dev.mysql.com/doc/refman/5.0/en/join.html . 该文档可以在这里找到: http : //dev.mysql.com/doc/refman/5.0/en/join.html

I'll leave it as an exercise to figure out how to JOIN the order_from . 我将把它作为练习来弄清楚如何JOIN order_from

暂无
暂无

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

相关问题 从一行获取基于ID的两个用户名 - getting two usernames based on ids from one row 只根据id在mysql php中获得一个表行 - Only get one occurance of a table row in mysql php based on id mysql-根据ID和DATE从一个表中获取一行,如果不显示空值,则在另一表中获取多行 - mysql - taking one row from one table based on ID and DATE and get multiple rows in another table if not show null values 从一个MySQL表的两行获取ID,然后使用PHP在另一个MySQL表的一行中输入这些值 - Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP PHP:从一个 mysql 表中获取最后一行的 id 号,并使用它来更新另一个表的第一行中的字段 - PHP: Taking the last row's id number from one mysql table and using it to update a field in the first row of another table MySQL查询:引用另一个表的最后插入行(仅使用一个php-SQL字符串) - MySQL-Query: Refer to last inserted row from another table (just with one php-SQL String) php + mysql:将一行从一个表复制到另一个表 - php+mysql: copying a row from one table to another 在MySQL php中将ID从一个表插入另一个表 - Insert ID from one table to another in MySQL php 如何使用PHP查询MySQL的某些行,然后针对每一行,基于一个UID,基于值查询另一个表? - How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value? MySQL PHP ID在一个表中而不是另一个表中 - MySQL PHP ID in one table and not another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM