我有一個與MYSQL關係的小問題。MYSQL和PHP其中任何相關行包含字符串
沒有爲表1中的每個值1,可以在表中的值(0+)的衆多2.
我能夠正確地得到所有的數據,但是,問題來當一些表2中的值有所不同,特別是「已採納」字段。
$sql = "
SELECT
accounts.name AS business,
accounts.industry AS style,
accounts_cstm.renewaldate_c AS ren_date,
accounts_cstm.nolongercontact_c AS NLC,
accounts_cstm.contactname_c AS person,
campaigns.name AS campaign,
users.first_name AS exec_fn,
users.last_name AS exec_sn,
email_addr_bean_rel.bean_id AS bean_id,
email_addresses.email_address AS email,
qs_quotationinformation.takenup AS takeup,
email_addr_bean_rel.email_address_id AS email_id
FROM
accounts
LEFT JOIN
campaigns ON accounts.campaign_id = campaigns.id
LEFT JOIN
users ON accounts.assigned_user_id = users.id
INNER JOIN
accounts_cstm ON accounts.id = accounts_cstm.id_c
LEFT JOIN
email_addr_bean_rel ON accounts.id = email_addr_bean_rel.bean_id
LEFT JOIN
email_addresses ON email_addr_bean_rel.email_address_id = email_addresses.id
LEFT JOIN
qs_quotamation_accounts_c ON accounts.id = qs_quotamation_accounts_c.qs_quot108funts_ida
LEFT JOIN
qs_quotationinformation ON qs_quotamation_accounts_c.qs_quotdb81tion_idb = qs_quotationinformation.id
WHERE
accounts.deleted = 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if($row["NLC"] == 1 || $row["takeup"] == 1){$NLC = "No";}else{$NLC = "Yes";}
echo '<tr><td>'.$row['business'].'</td><td>'.$row["style"].'</td><td>'.$row["ren_date"].'</td><td>'.$NLC.'</td><td>'.$row["person"].'</td><td>'.$row["campaign"].'</td><td>'.$row["exec_fn"].' '.$row["exec_sn"].'</td><td>'.$row["email"].'</td><td>'.$row["takeup"].'</tr>';
}
} else {
echo "0 results";
}
在這種情況下,表1是「帳戶」,表2是「qs_quotationinformation」。
有表1中某些帳戶具有在表2中的多個記錄,和一些在表2中的「takenup」記錄(與同一賬戶)可以是1和一些爲0
所以我需要做的就是讓表2中的任何記錄= 1,那麼$ NLC的所有變量都需要=「No」。
我不知道這是否可能,或者是否有更好的方法來獲取此信息。 HTML表格缺少被拉取的數據,但這是因爲該表格只是用戶最重要數據的直觀表示。
編輯表格(不包括個人數據):
表1: +----+---------+---------+ | id | name | deleted | +----+---------+---------+ | 1 | example | 0 | +----+---------+---------+
表2: +----+---------+ | id | takenup | +----+---------+ | 1 | 0 | +----+---------+ | 2 | 1 | +----+---------+ | 3 | 0 | +----+---------+
所有表2中的行的涉及到行於表1。但是,由於存在NLC需要返回「否」而不是「是」(它當前所做的是因爲最後一個相關行是0)
只是一個提示,儘量保持儘可能低的聯接,7是很多。 – Grumpy
你能否提供你的樣本數據和表結構 – Beginner
@Grumpy不幸的是,我必須使用的數據庫有1個帳戶的記錄分裂了超過10個表,由於創建/使用它的程序 – Jordan