gpt4 book ai didi

mySQL 从两个表中选择并匹配结果

转载 作者:行者123 更新时间:2023-11-29 07:07:00 25 4
gpt4 key购买 nike

我愿意

SELECT DISTINCT column1, column2 as t1

我愿意

SELECT column3 FROM table2 WHERE column4 = "1" AND column3 = (matches)t1.column1

我的查询应该是什么样子?

SELECT DISTINCT column1, column2 FROM table1 as t1,
column3 FROM table2 WHERE column4 = "1" AND column3 = t1.column1

结果应该是一个包含第 1 列数据的列表,与第 3 列和第 4 列 = 1 的数据相匹配

Table1:                Table2:
-----------------------------------------
column1 column2 column3 column4
-----------------------------------------
001 Name 001 1
002 Name 002 0
003 Name 004 1
005 Name 005 1
-----------------------------------------
Result: 001, 005

错误:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM table2 WHERE column4 = '1' AND column3 = t1.column1' at line 1

最佳答案

select distinct
t1.column1,
t1.column2,
t2.column4
from
table1 as t1
inner join
table2 as t2 on t2.column3 = t1.column1
where
column4 = "1"

关于mySQL 从两个表中选择并匹配结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41279937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com