合并两个MySQL查询并替换结果

时间:2015-02-12 15:38:38

标签: mysql

我想找到一种方法来使用一个查询,当它取代LineUps.DIG输出时它= Y.我想在html中标记为星号。也许最好在前端vs sql查询中执行此操作。

Fist查询有效并返回数据

$stmt = $conn->prepare("SELECT distinct Channel_LineUps.channel, Channel_LineUps.description, Channel_LineUps.Tier, LineUps.HD, LineUps.DIG FROM Channel_LineUps, LineUps WHERE (LineUps.Market_ID = Channel_LineUps.Market_ID AND Channel_LineUps.Market_ID = 28) ORDER BY Channel_LineUps.Tier ASC"); 

第二个查询是我有替换的地方,但我不确定如何与上面的查询合并。

SELECT DIG,REPLACE(DIG,"Y","*") as output FROM LineUps WHERE DIG="Y");

1 个答案:

答案 0 :(得分:0)

我认为CASE WHEN正是您所寻找的:

$sql = <<<SQL
SELECT DISTINCT Channel_LineUps.channel, Channel_LineUps.description,
Channel_LineUps.Tier, LineUps.HD,
CASE LineUps.DIG WHEN "Y" THEN REPLACE(LineUps.DIG, "Y", "*") ELSE LineUps.DIG END
FROM Channel_LineUps, LineUps
WHERE (LineUps.Market_ID = Channel_LineUps.Market_ID
AND Channel_LineUps.Market_ID = 28)
ORDER BY Channel_LineUps.Tier ASC
SQL;

$stmt = $conn->prepare($sql);

您也可以直接将以上第4行替换为:

CASE LineUps.DIG WHEN "Y" THEN "*" ELSE LineUps.DIG END

你肯定知道它的价值。