如果變量$ whereEmail或變量$ whereNotEmail或兩者均爲空,則以下查詢不會選擇任何行。
$whereEmail = "'[email protected]','[email protected]'";
$whereNotEmail = "'[email protected]','[email protected]'";
$result = $mSearch->dao->query(sprintf('
SELECT * FROM table
WHERE email IN (%s)
AND email NOT IN (%s)
', $whereEmail, $whereNotEmail));
變量$ whereEmail和$ whereNotEmail中的電子郵件地址是使用php函數存儲的,在某些情況下,它們留空或爲空。
我希望如果變量$ whereEmail和$ whereNotEmail都爲空,查詢應該從表中選擇所有行。
同樣,我期望如果變量$ whereEmail爲空,查詢應該選擇表中的所有行,除了那些電子郵件地址在變量$ whereNotEmail中的行。
同樣,我期望如果變量$ whereNotEmail爲空,查詢應該選擇電子郵件地址在變量$ whereEmail中的所有行。
什麼http://stackoverflow.com/questions/19357990/sql-select-all-if-parameter-is-null-else-return-specfic-item它會對你有幫助嗎? – Syed
@Syed,不會因爲它檢查SQLParameter而不是PHP變量。你瞭解差異嗎? – Rahul
不,可以將PHP變量轉換爲SQL參數嗎? – Syed