简体   繁体   English

选择表格A,表格B中的值

[英]Select form table A where value in table B

Table A 表A

id    name
1     name1
2     name2
3     name3
4     name4

Table B 表B

id    userlist
1     1,2,3,4

What I do now is 我现在要做的是

SELECT `userlist` FROM `table B` WHERE `id` = 1

Then save the result and put into second query 然后保存结果并放入第二个查询

SELECT `name` FROM `table A` WHERE `id` in ($userlist)

Just wondering if got better way. 只想知道是否有更好的方法。 I try 我尝试

SELECT `name` FROM `table A` WHERE `id` in (SELECT `userlist` FROM `table B` WHERE `table B`.`id` = 1)

But it only return 1 result. 但是它只返回1个结果。

EDIT* 编辑*

DB structure can not be change since so many data inside. 因为里面有这么多数据,所以不能更改数据库结构。 I just try to optimize the old code. 我只是尝试优化旧代码。

You could use FIND_IN_SET : 您可以使用FIND_IN_SET

SELECT `name`
FROM `table A` INNER JOIN `table B` ON
     FIND_IN_SET(`table A`.`id`, `table B`.`userlist`)
WHERE `table B`.`id` = 1

but i would suggest you to normalize your tables. 但我建议您标准化表格。

The proper way to solve this is changing the table definition and using JOINs. 解决此问题的正确方法是更改​​表定义并使用JOIN。 For example 例如

Table A 表A

id    name    b_id
1     name1   1
2     name2   1
3     name3   1
4     name4   1

Table B 表B

id    
1  

To get names from table A that have B id 1 you would write: 要从表A中获得具有B id 1的名称,您应该编写:

select name from A join B on A.b_id = B.id where B.id = 1

Or, if you can't change Table A, define a new table to maintain the relationship AB: 或者,如果您不能更改表A,则定义一个新表以维护关系AB:

Table A 表A

id    name
1     name1
2     name2
3     name3
4     name4

Table B 表B

id    
1     

Table A_B 表A_B

a_id b_id
1    1
2    1
3    1
4    1

SQL to extract data: SQL提取数据:

select name from A join A_B on A.id = A_B.b_id where A_B.b_id = 1

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

相关问题 从表中选择A列或B列中的值 - Select from table where value in column A or column B 从表中选择 [列 a],其中没有 [列 b = 任意值] 的实例 - Select [column a] from table where there are no instances of [column b = arbitrary value] 在SQL中,从表a中选择全部,其中列a的值等于表b中最新条目的值? - In SQL, select all from table a where column a value equals the value of the most recent entry in table b? select from table where col_a in() ONLY where col_b value match - select from table where col_a in() ONLY where col_b value matches 从表A和B中选择,其中表A和表B中的记录在表C中不存在? - Select from table A and B, where records from table A and table B don't exist in table C? 仅在表B中没有值的情况下连接表 - Join tables only where value not in table B mysql select * from table from table where column a value = column b value laravel - mysql select * from table where column a value = column b value laravel 表 A 中的 mysql select(在条件下)不在表 B 中(在相同条件下) - mysql select from Table A (on conditions) where not in Table B (on same conditions) 选择表B中与表A匹配的所有值 - Select all values from table B where matches table A mysql select from table B where条件不满足直接在表B上,而是通过表A上的join - mysql select from table B where condition is not met on table B directly, but through join on table A
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM