简体   繁体   English

在MySQL查询中使用Array结果

[英]Using Array results in MySQL query

I have the following query: 我有以下查询:

$query_q_pass = sprintf("SELECT * FROM answers INNER JOIN users WHERE a_id = %s and answers.user_id = users.user_id");
$q_pass = mysql_query($query_q_pass, $cd) or die(mysql_error());
$row_q_pass = mysql_fetch_assoc($q_pass);

Now the answers table from above query has a row brand_url which has many different values separated with commas, eg dell, microsoft, hp, ... 现在上面查询的答案表有一行brand_url ,其中有许多不同的值用逗号分隔,例如dell,microsoft,hp, ...

What I want to do is select all those values from the brand_url row and add them in another query where I will extract all brands that exist inside brand_url . 我想要做的是从brand_url行中选择所有这些值,并将它们添加到另一个查询中,我将提取brand_url中存在的所有品牌。 This is the other query: 这是另一个查询:

$query_conn_brands = 'SELECT * FROM brands WHERE brand_url = 'VALUE FROM ABOVE QUERY'';
$conn_brands = mysql_query($query_conn_brands, $cd) or die(mysql_error());
$row_conn_brands = mysql_fetch_assoc($conn_brands);
$totalRows_conn_brands = mysql_num_rows($conn_brands);

I tried many diferent ways using arrays but all I get is all results from the brands table. 我尝试了许多使用数组的不同方法,但我得到的只是品牌表的所​​有结果。 This is my last unsuccessful try: 这是我最后一次不成功的尝试:

$brand_pass_ids = $row_q_pass['brand_id'];
$array = array($brand_pass_ids,);

$query_conn_brands = 'SELECT * FROM brands WHERE brand_url IN (' . implode(',', array_map('intval', $array)) . ')';
$conn_brands = mysql_query($query_conn_brands, $cd) or die(mysql_error());
$row_conn_brands = mysql_fetch_assoc($conn_brands);
$totalRows_conn_brands = mysql_num_rows($conn_brands);

From the above example all I get is ALL results from brands table and not only those that exist inside answers table. 从上面的例子我得到的是品牌表的所​​有结果,而不仅仅是答案表中存在的结果。 I really hope someone will help me with this. 我真的希望有人会帮助我。

Thanks a lot! 非常感谢!

The reason it's failing is because your query will look like: 它失败的原因是因为您的查询看起来像:

SELECT * FROM brands WHERE brand_url IN (dell,microsoft,hp)

There are string values, and so they must be handled as strings. 有字符串值,因此必须将它们作为字符串处理。 Try: 尝试:

implode(",",array_map(function($a) {return json_encode($a);},$array))

Although encoding as JSON may seem crazy, it's actually a good choice here. 虽然编码为JSON可能看起来很疯狂,但它实际上是一个不错的选择。 Strings are wrapped in quotes and escaped as needed, numbers are left as they are, and null is also handled correctly. 字符串用引号括起来并根据需要进行转义,数字保持原样,并且null也可以正确处理。

EDIT: For older versions of PHP that don't support anonymous functions: 编辑:对于不支持匿名函数的旧版PHP:

implode(",",array_map(create_function('$a','return json_encode($a);'),$array));

Use MySQL's FIND_IN_SET() function: 使用MySQL的FIND_IN_SET()函数:

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. 如果字符串str位于由N个子字符串组成的字符串列表strlist中,则返回1到N范围内的值。 A string list is a string composed of substrings separated by “ , ” characters. 字符串列表是由用“ , ”字符分隔的子字符串组成的字符串。 If the first argument is a constant string and the second is a column of type SET , the FIND_IN_SET() function is optimized to use bit arithmetic. 如果第一个参数是常量字符串而第二个参数是SET类型的列,则FIND_IN_SET()函数被优化为使用位算术。 Returns 0 if str is not in strlist or if strlist is the empty string. 如果str不在strlist或者strlist是空字符串,则返回0 Returns NULL if either argument is NULL . 返回NULL ,如果任何一个参数为NULL This function does not work properly if the first argument contains a comma (“ , ”) character. 如果第一个参数包含逗号(“ , ”)字符,则此函数无法正常工作。

 mysql> SELECT FIND_IN_SET('b','a,b,c,d'); mysql> SELECT FIND_IN_SET('b','a,b,c,d');\n        -> 2  - > 2 

In your case: 在你的情况下:

SELECT * FROM brands WHERE FIND_IN_SET(brand_url, ?)

But you could join your queries together if so desired: 但如果需要,您可以一起加入您的查询:

SELECT *
FROM   answers
  JOIN users  USING (user_id)
  JOIN brands ON FIND_IN_SET(brands.brand_url, answers.brand_url)
WHERE a_id = %s

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

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