简体   繁体   English

PHP mysqli SELECT * from my_table哪里ID = array_of_ids

[英]PHP mysqli SELECT * from my_table WHERE id = array_of_ids

I have been using this site for years now and this is the first time I'm asking a question here, so kinda scared right now :D 我已经使用这个网站多年了,这是我第一次在这里提问,所以现在有点害怕:D

Here's what my problem is, I have got two tables. 这是我的问题,我有两个桌子。 In table_a I got three columns and in table_b I got 5. So the setup right now looks something like this: 在table_a中,我有三列;在table_b中,我有5列。因此,现在的设置如下所示:

       table_a

| r_id | foo   | bar |
+------+-------+-----+
| 1    | dude  | 5   |
+------+-------+-----+
| 2    | homie | 6   |
+------+-------+-----+
| 3    | bro   | 7   |
+------+-------+-----+

       table_b

| id | ada   | rea | lm   | cor  |
+----+-------+-----+------+------+
| 5  | ching | ink | jk   | 32.4 |
+----+-------+-----+------+------+
| 1  | momo  | pal | lmao | 95.5 |
+----+-------+-----+------+------+
| 6  | mama  | pen | lol  | 26.9 |
+----+-------+-----+------+------+
| 4  | chac  | pin | fun  | 91.2 |
+----+-------+-----+------+------+
| 7  | chim  | lap | funk | 82.4 |
+----+-------+-----+------+------+
| 9  | cho   | kil | fin  | 38.1 |
+----+-------+-----+------+------+

Now what I'm trying to do is to get all the data from table_a and then only get lm from table_b. 现在,我要做的是从table_a获取所有数据,然后仅从table_b获取lm I'm getting all the data from table_a like this: 我从table_a获得所有数据,如下所示:

SELECT r_id, foo, bar from table_a

I need to use the ids I get from bar column to get lm from table_b. 我需要使用从bar列获取的ID从table_b获取lm So is there a way I can pass an array to only get the data based on the ids in an array? 那么有没有办法我可以传递一个数组,使其仅基于数组中的id获取数据? If not, then what would be the most efficient way to get those? 如果没有,那么获得这些的最有效方法是什么?

The output I'm expecting is jk, lol, funk . 我期望的输出是jk, lol, funk Would appreciate any help, thanks! 希望得到任何帮助,谢谢!

You should be looking at using a JOIN to link the two tables together in 1 query... 您应该在1个查询中使用JOIN将两个表链接在一起...

SELECT  r_id, foo, bar, lm
    FROM table_a 
    JOIN table_b on bar = id

Why not join? 为什么不参加?

select group_concat(lm) as lm_list
from table_b b
inner join table_a a on b.id = a.bar

You can use the GROUP_CONCAT() function, with this you would get jk, lol, funk otherwise you would get 3 rows each of one lm value, 您可以使用GROUP_CONCAT()函数,通过此函数,您将得到jk, lol, funk否则,您将获得3行,每行1 lm值,

For that you can try WHERE IN feature of SQL. 为此,您可以尝试SQL的WHERE IN功能。

SELECT lm from table_b WHERE id IN(ARRAY_OF_IDS)

Or you can also use join to achieve this 或者您也可以使用join来实现

Select tale_a.*, tale_b.lm from tale_a inner join table_b ON tale_a.bar=tale_b.id

尝试inner join

SELECT a.r_id, a.foo, a.bar, b.lm  from table_a as a inner join table_b as b on b.id=a.bar

You can use INNER JOIN 您可以使用INNER JOIN

 SELECT tale_a.r_id, tale_a.foo, tale_a.bar ,table_b.lm 
 FROM tale_a
 INNER JOIN table_b
 ON tale_a.bar=table_b.id

Note: It returns all columns from table_a and only one column from table_b 注:从返回的所有列table_a和只有一个列table_b

Resultant Output: 结果输出:

| r_id | foo   | bar | lm  |
+------+-------+-----+-----+
| 1    | dude  | 5   | jk  |
+------+-------+-----+-----+
| 2    | homie | 6   |lol  |
+------+-------+-----+-----+
| 3    | bro   | 7   |funk |
+------+-------+-----+-----+

I assume that you have array of IDs having IDs. 我假设您有具有ID的ID数组。 So first make a comma separated string of that array of IDs like this: 因此,首先用逗号分隔该ID数组的字符串,如下所示:

ids_str = implode("," $ARRAY_OF_IDS);

and then use that ids_str in IN os mysql query like below: 然后在IN os mysql查询中使用该ids_str ,如下所示:

SELECT lm from table_b WHERE id IN( ids_str )

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

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