简体   繁体   English

如何从基于其他行和列值的列中选择一个值?

[英]How to select a value from a column which is based on other row and column value?

Well, best explained with a example: 好吧,最好用一个例子解释一下:

MYSQL TABLE NAME: CLIENTS
ID        NAME        CODE
1         Mark        0
2         Joe         1

What I want: 我想要的是:

HTML TABLE:
MATCH        NAME
Mark         Joe

Did you get it? 你明白了吗? Like, I need to take the NAME from the person who has a ID equal to the CODE of other. 就像,我需要从ID等于其他CODE的人那里获得NAME。

I think that you won't need my code, since I just want to know the function that allows me to echo two distinct "NAMES". 我认为您不需要我的代码,因为我只想知道允许我回显两个不同的“ NAMES”的函数。 Like, if I call two echo $name; 就像,如果我叫两个echo $name; how the code knows who is who? 代码如何知道谁是谁?

Ps.: I'm not a back-end developer, take it easy =) 附注:我不是后端开发人员,请放轻松=)
Ps².: It is a loop (what I know to do hehehehe), it has to repeat that "search" and list a lot of "MATCHES", you know? Ps².:这是一个循环(我知道该怎么做),它必须重复“搜索”并列出很多“ MATCHES”,您知道吗?

Using Mysql JOIN 使用Mysql JOIN

SELECT b.name, a.name as match_name
FROM clients a
JOIN clients b
ON a.id = b.code;

Output 输出量

+------+------------+
| name | match_name |
+------+------------+
| Joe |     Mark    |
+------+------------+

we can find reord in the same table by using self join... 我们可以使用self join在同一张表中找到ord

SELECT e.NAME as parent, m.NAME as child FROM CLIENTS e, CLIENTS m WHERE e.ID = m.CODE

Hope this will be your solution 希望这将是您的解决方案

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

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