查询uservariable列表的表

时间:2022-10-17 13:25:00

I have the following Problem: I have a table table1 with multiple columns col1 through col2:

我有以下问题:我有一个表表表1,其中有多个列col1到col2:

+------+------+  
| col1 | col2 |    
+------+------+  
|  1   |  4   |  
|  2   |  4   |  
|  3   |  4   |  
|  4   |  5   |  
+------+------+

Now I need all entries col1-entries with a four in in col2, for which I use:

现在我需要所有的col1-entries和col2中的4,我使用的是:

SELECT @col2fours := col1 FROM table1 WHERE col2 = 4;

Now I have a table2 where I want to set the rows where col3 has one of the values stored in @col2fours. For this I use:

现在我有了一个表2,我想在其中设置col3在@col2fours中存储一个值的行。我使用:

SELECT * ID FROM table2 WHERE col3 IN @col2fours; 

This doesn't work. I already tried = and having instead of in as well as using Group_concat. Nothing worked for me so far.
I would like to do this directly in mySQL because the database lies on a server and I want to reduce accesses to the database as much as possible. The real problem has somewhat more tables involved than 2 ;). Appreciate any input, thanks.

这并不工作。我已经尝试了=和have而不是in以及Group_concat。到目前为止,还没有什么对我起作用。我想在mySQL中直接这样做,因为数据库位于服务器上,我想尽可能减少对数据库的访问。真正的问题涉及的表比2要多;)欣赏任何输入,谢谢。

2 个解决方案

#1


1  

you can use sub query like this directly

可以像这样直接使用子查询

SELECT * FROM table2 WHERE col3 IN (SELECT col1 FROM table1 WHERE col2 = 4); 

#2


1  

This approach is a no-go, since in MySQL there is no array data type, so a variable can hold only 1 value at a time.

这种方法是不允许的,因为在MySQL中没有数组数据类型,所以一个变量一次只能保存一个值。

Just use a subquery or join to achieve the expected results:

只需使用子查询或连接来实现预期的结果:

SELECT * FROM table2 WHERE col3 IN (SELECT col1 FROM table1 WHERE col2 = 4)

SELECT table2.* FROM table2
INNER JOIN table1 ON table2.col3=table1.col1
WHERE table1.col2 = 4

#1


1  

you can use sub query like this directly

可以像这样直接使用子查询

SELECT * FROM table2 WHERE col3 IN (SELECT col1 FROM table1 WHERE col2 = 4); 

#2


1  

This approach is a no-go, since in MySQL there is no array data type, so a variable can hold only 1 value at a time.

这种方法是不允许的,因为在MySQL中没有数组数据类型,所以一个变量一次只能保存一个值。

Just use a subquery or join to achieve the expected results:

只需使用子查询或连接来实现预期的结果:

SELECT * FROM table2 WHERE col3 IN (SELECT col1 FROM table1 WHERE col2 = 4)

SELECT table2.* FROM table2
INNER JOIN table1 ON table2.col3=table1.col1
WHERE table1.col2 = 4