选择CASE缺少的东西

时间:2012-05-01 07:12:05

标签: php sql select

你好,我需要做这样的事情

检查列的vaule,如果vaule为1,则$ display_output为“something” 如果vaule是2,则$ display_output是“other”

到目前为止,我有这个

$sql_istorrenthere = $this->query("SELECT CASE WHEN torrent_download ='1' THEN $display_output = GMSG_NOTORRENT ELSE $display_output = GMSG_TORRENT ; FROM" . DB_PREFIX . "auctions"

2 个答案:

答案 0 :(得分:1)

你不能设置这样的变量。您可以将torrent_download的值设置为“something”或“other”:

$sql_istorrenthere = $this->query(
    "SELECT 
      CASE WHEN torrent_download = '1' 
        THEN 'something' 
        ELSE 'other' 
      END AS torrent_download
    FROM" . DB_PREFIX . "auctions"
);

答案 1 :(得分:0)

SELECT IF(STRCMP(torrent_download,'1'),'GMSG_TORRENT','GMSG_NOTORRENT') 
AS display_output
FROM " . DB_PREFIX . "auctions";

然后在PHP中,你可以这样做:

$response = $sql_istorrenthere -> fetch_assoc();
$display_output = $response['display_output'];

但是有更好的方法可以做到这一点,其中没有一个涉及尝试将变量名称设置为查询本身。