在mysql行中拆分字符串

时间:2015-09-10 19:37:12

标签: php mysql

我一直在网上搜索,找到了解决问题的“差不多”解决方案。 我正在使用查询:'MYSQL'

SELECT distinct company,price,material,`contractors_parameters`.`width`,`contractors_parameters`.`height`
from `contractors_parameters`
join `Windows_last_submissions` `wls`
where `wls`.`width` in (SELECT  SPLIT_STR(`contractors_parameters`.`width` /*string*/
                                           , ',' /*delimiter*/
                                           , 1 /*start_position*/
                                           ))
and `wls`.`height` in (SELECT SPLIT_STR(`contractors_parameters`.`height` /*string*/
                                           , ',' /*delimiter*/
                                           , 1 /*start_position*/
                                           ))
and `contractors_parameters`.`material` = `wls`.`chosenmaterial`
and `contractors_parameters`.`type` = `wls`.`type`
and `contractors_parameters`.`price` <= `wls`.`maximumbid`
ORDER BY `contractors_parameters`.`company` ASC

在我的“width”和“height”列中。因此可能有多个整数:“22,23,24,25”。我使用了SPLIT_STR函数(我猜不是小提琴)将这些数字分开作为单独的字符串,并使用IN子句再次查询它们。
 它几乎可以工作,除非'contractors_parameters'中的尺寸是宽度= 24,25高度= 48,49,50和'Windows_last_submission'中的尺寸(这是我要查询的表) - 它将起作用..但如果'contractors_paramaters'的尺寸是相反的,或者以不同的顺序,比如宽度= 25,24高度= 48,49,50 - 它将无法工作。 有人有什么建议吗?我尝试使用小提琴,但是当我使用SPLIT_STR()函数时它会出错。

2 个答案:

答案 0 :(得分:1)

撇开你的数据不应该以那种方式存储的事实......你的检索方法应该没问题,而不必在查询中拆分字符串。

一旦你得到它就可以用它来分割它(并且它可以在小提琴中工作)

var str = "23,24,25";
var res = str.split(",");

如果您不喜欢JS而宁愿将其保留给PHP:

<?php
// Delimiters may be slash, dot, or hyphen
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>

这比在SQL级别尝试这样做容易得多。

答案 1 :(得分:1)

您的STR_SPLIT函数仅返回一个值,而不是您期望的值数组。如果您绝对必须使用此结构中的数据,那么您将其工作的唯一方式就是以下可疑查询

SELECT distinct company,price,material,`contractors_parameters`.`width`,`contractors_parameters`.`height`
from `contractors_parameters`
join `Windows_last_submissions` `wls`
where `wls`.`width` in (
      SELECT  SPLIT_STR(`contractors_parameters`.`width` /*string*/
                                           , ',' /*delimiter*/
                                           , 1 /*start_position*/)
                                           union
      SELECT  SPLIT_STR(`contractors_parameters`.`width` /*string*/
                                           , ',' /*delimiter*/
                                           , 2 /*start_position*/)
                                           union
      SELECT  SPLIT_STR(`contractors_parameters`.`width` /*string*/
                                           , ',' /*delimiter*/
                                           , 3 /*start_position*/)
                                           union
      SELECT  SPLIT_STR(`contractors_parameters`.`width` /*string*/
                                           , ',' /*delimiter*/
                                           , 4 /*start_position*/)
                                           )
and `wls`.`height` in (
    SELECT SPLIT_STR(`contractors_parameters`.`height` /*string*/
                                           , ',' /*delimiter*/
                                           , 1 /*start_position*/)
                                           union
    SELECT SPLIT_STR(`contractors_parameters`.`height` /*string*/
                                           , ',' /*delimiter*/
                                           , 2 /*start_position*/)
                                           union
    SELECT SPLIT_STR(`contractors_parameters`.`height` /*string*/
                                           , ',' /*delimiter*/
                                           , 3 /*start_position*/)
                                           union
    SELECT SPLIT_STR(`contractors_parameters`.`height` /*string*/
                                           , ',' /*delimiter*/
                                           , 4 /*start_position*/)
                                           )
and `contractors_parameters`.`material` = `wls`.`chosenmaterial`
and `contractors_parameters`.`type` = `wls`.`type`
and `contractors_parameters`.`price` <= `wls`.`maximumbid`
ORDER BY `contractors_parameters`.`company` ASC;

您需要在contractors_parameters.width或height列中包含您期望的最大逗号分隔值的联合行。

相关问题