我剛剛意識到我可能錯過了一些我的疑問。在我的網站的某些部分我有一個從同一個表的同一列的不同行檢索值$ SQL變量,但必須在WHERE子句中不同的變量:Select statement with multiple rows/same column - WHERE has multiple variables and FROM FROM same table
$sql1 = "SELECT login_id FROM login WHERE login_id = '$login_id1'";
$sql2 = "SELECT login_id FROM login WHERE login_id = '$login_id2'";
$result1 = mysqli_fetch_assoc(mysqli_query($con,$sql1));
$result1a = $result1['login_id'];
$result2 = mysqli_fetch_assoc(mysqli_query($con,$sql2));
$result2a = $result2['login_id'];
因爲我來自同一個表中選擇,是不是有一些方法來結合$ sql1和$ sql2以及$ result變量,如下所示?
$sql = "SELECT login_id FROM login WHERE login_id = '$login_id1' AS 'first', login_id =
'$login_id2' AS 'second'";
$result = mysqli_fetch_assoc(mysqli_query($con,$sql));
$result1 = $result['first']; //uses $login_id1
$result2 = $result['second']; //uses $login_id2
另一種方法是這樣的:
$sqlAlt = "SELECT login_id FROM login WHERE (login_id = '$login_id1' OR login_id =
'$login_id2')";
只是我不知道我怎麼會從$ sqlAlt(使用mysqli_fetch_assoc或者是一些其他的功能)只提取LOGIN_ID使用變量$ login_id1或$ login_id2;像
$result1 = $sqlAlt['login_id'] //WHERE $result1 is from $login_id1
$result2 = $sqlAlt['login_id'] //WHERE $result2 is from $login_id2
感謝您的幫助!
此查詢返回列名爲FIRST的兩行。這似乎不是OP要求的。 –
好的,謝謝...我會試試這個。出於好奇,是你寫的數據庫查詢的$ sql語句,還是UNION是一個合併(在本例中)2個數據庫查詢的命令? –
將兩個查詢合併爲一個:將它們作爲UNION ALL執行只會涉及1次通過數據庫而不是兩次。 –