[英]Sql query on taking the value from a case within select statement in a CTE
[英]SQL SELECT CASE with Where From Looping Value
我有 2 個表:表名交易和詳細客戶
事務表字段是 idtrans 和 idcust。
詳細客戶字段是 idcust 和 custname。
我的 php 語法有問題,我想要 select 表,條件來自值循環,這是我的代碼:
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="select * from transaction";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$sqlid="select * from detailcust where idcust='$row[idcust]')";
$resultid= $conn->query($sqlid);
if ($resultid->num_rows > 0){
while ($rowid= mysqli_fetch_array($resultid)){
$custname=$rowid["custname"];
echo $custname;
}
結果名稱始終是第一個 idcust 值。
您似乎想要至少有一筆交易的客戶姓名。 如果是這樣,則不需要循環和多個查詢。 只需一個查詢即可獲得所需的結果:
select c.*
from detailcust c
where exists (select 1 from transaction t where t.idcust = c.idcust)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.