简体   繁体   English

匹配来自两个不同表的两个不同列

[英]match two different column from two different tables

i have two tables: table1 and table2 using database mysql. 我有两个表:使用数据库mysql的table1和table2。

Fields of table1: usrnm1, ques1 Fields of table2: usrnm2, ques2 表1的字段:usrnm1,ques1表2的字段:usrnm2,ques2

Data of table1: abc, xyz Data of table2: pqr, def 表1的数据:abc,xyz表2的数据:pqr,def

In html form, i am taking username from the user. 以html格式,我从用户那里获取用户名。 I want to retrieve corresponding ques in other input tag. 我想在其他输入标签中检索相应的问题。

How can i check the input(by user) in both these tables and retrieving corresponding ques. 我如何检查这两个表中的(按用户)输入并检索相应的查询。

I want these type of output: If user enter abc then i want xyz as output. 我想要这些类型的输出:如果用户输入abc,那么我希望xyz作为输出。 And if user enter pqr then i want def as output. 如果用户输入pqr,那么我想将def作为输出。

You can combine 2 table if you rename the fields so that they match. 如果您重命名字段以使其匹配,则可以合并2个表。

So you can either do it like this: 因此,您可以这样做:

(SELECT usrnm1 as usrnm, ques1 as ques FROM table1 WHERE usrnm1 = "abc")
UNION
(SELECT usrnm2 as usrnm, ques2 as ques FROM table2 WHERE usrnm2 = "abc");

Or like this: 或像这样:

SELECT * FROM (
  (SELECT usrnm1 as usrnm, ques1 as ques FROM table1)
  UNION
  (SELECT usrnm2 as usrnm, ques2 as ques FROM table2)
) a WHERE usrnm = "abc";

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

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