下面是代碼:是否可以使用mysql將結果添加到結果中?
// connect and select the database
$smt = $db->query(" SELECT 'one' which, col1, col2 FROM table1 WHERE col1 = '10'
UNION ALL
SELECT 'two' which, col1, col2 FROM table2 WHERE col1 = '10'
");
while($result = $smt->fetch()) {
if($result['which'] == 'one'){
$result['col1'] = 'String: '.$result['col1'];
}
}
正如你在上面的代碼中看到,有一個字符串的col1
(只是table1
)結果相結合的IF()
聲明。現在我想知道,是否可以使用MySQL來做到這一點? (目前我使用PHP來做)
換句話說,我可以在MySQL中做到這一點嗎?
if($result['which'] == 'one'){
$result['col1'] = 'String: '.$result['col1'];
}
是的,案例陳述。搜索一下。 –
@JorgeCampos emm,是的,我想我可以使用'Case'語句來做到這一點,謝謝 – Shafizadeh
使用['IF'](https://dev.mysql.com/doc/refman/5.0/en/if.html)和['CONCAT'](https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat)就像這樣:'IF(which =「one」,CONCAT(「String: 「,col1),col1)AS col1'。 – h2ooooooo