SQL - 字符串逻辑

时间:2015-11-19 05:34:04

标签: sql arrays string logic

我不知道我想要实现的是可能的,所以这是我的难题。

在SQL表中,有许多字段在字符串中包含yes / no标志,例如。在该字段上可以称为“Stock”,并且在该一个字段内有一串标志,例如, “YNYYY”可以说例如标志代表。

  • 芬达
  • 百事可乐
  • 轻快
  • Dr Pepper

在这种情况下,我希望在返回数据时返回Coke,Pepsi,Lilt,Dr Pepper,省略了Fanta。

现在可以使用CASE语句,这可能是我必须使用的答案,但理想情况下,我不必编写数百个不同的变量,任何人都知道这可以实现的方式吗?

你的帮助一如既往地赞赏,我已经完成了正常的谷歌搜索,也许我根本不知道要搜索什么,因为它给我空白。

请指出正确的方向。

此致

[R

3 个答案:

答案 0 :(得分:0)

为什么不将SELECTWHERE一起使用?

像这样。

SELECT GROUP_CONCAT(`Stock`) 
 FROM table_name 
 WHERE `flag` = 'Y'

希望这有帮助。

答案 1 :(得分:0)

实现目标的一种方法是编写一个表值函数,将<?php // Build an array holding all of the data from each table $rowNum = 0; // Start the count while ($row1 = mysql_fetch_array($result1)) { //plants $data[$rowNum]['col1']['Nombre'] = $row1['Nombre']; $data[$rowNum]['col1']['idPlanta'] = $row1['idPlanta']; $rowNum++; } $rowNum = 0; // Restart the row count while ($row2 = mysql_fetch_array($result2)) { //user $data[$rowNum]['col2'] = $row2['username']; $rowNum++; } $rowNum = 0; // Restart the row count while ($row3 = mysql_fetch_array($result3)) { //email $data[$rowNum]['col3']['username'] = $row3['username']; $data[$rowNum]['col3']['email'] = $row3['email']; $rowNum++; } ?> <table align="center" align="top"> <tr> <th>Plants</th> <th>Users</th> <th>Emails</th> </tr> <?php // Run a foreach on the array to build out the table: foreach($data as $key => $row){?> <tr> <td> <?php if(is_array($row['col1'])){ echo '<div style="color:black;margin-left:50px;font-size:25px;">'; echo '<p> <a href="newPlanta.php?pltID='.$row['col1']['idPlanta'].'&viewer=true">Planta:'.$row['col1']['Nombre'].'</a></p>'; echo '</div>'; } ?> </td> <td> <?php if($row['col2']){ echo '<div style="color:black;margin-left:50px;font-size:25px;">'; echo '<p> <a href="perfil.php?id='.$row['col2'].'">Usuario:'.$row['col2'].'</a></p>'; echo '</div>'; } ?> </td> <td> <?php if(is_array($row['col3'])){ echo '<div style="color:black;margin-left:50px;font-size:25px;">'; echo '<p> <a href="perfil.php?id='.$row['col3']['username'].'">Usuario:'.$row['col3']['email'].'</a></p>'; echo '</div>'; } ?> </td> </tr> <?php } ?> </table> 字符串转换为Y/N表,然后根据约定加入查找表。以下是这样的事情:

(Id INT, value BIT)

然后,您将使用此函数加入CREATE FUNCTION udf_StringToBool(@intput varchar(100)) RETURNS @table TABLE ( Id INT IDENTITY(1,1), Value BIT ) AS begin declare @temp_input varchar(100) = @intput while len(@temp_input) > 0 begin insert into @table (value) SELECT CASE LEFT(@temp_input, 1) WHEN 'Y' THEN 1 ELSE 0 END set @temp_input = right(@temp_input, len(@temp_input)-1) END RETURN end (以及查找到产品)表,然后删除stock子句中没有库存的任何内容:

WHERE

以下是如何定义查找表:

SELECT s.*, v.Value, pl.Name
FROM 
     stock s 
     cross apply 
     ( 
         select b.* from udf_StringToBool(s.Flags) b
     ) v
     join product_lookup pl on pl.Id = v.Id
WHERE v.Value = 1

然后你可以使用PIVOT生成带有布尔值的列。

答案 2 :(得分:0)

最后我选择使用&#39; substring&#39;和案件&#39;声明,以便每个项目以这种方式出现在它自己的字段中,我减轻了编写每个变量的需要。

SELECT CASE WHEN SUBSTRING(STOCK,1,1) = 'y' THEN 'IN STOCK' ELSE 'OUTOFSTOCK' END AS COKE

我不知道为什么它没有发生在我的开始和没有你的提示和指导的情况下我可能已经做了很长一段时间,感谢所有人!