2016-09-29 57 views
0

我有一個與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)

+0

只是一個提示,儘量保持儘可能低的聯接,7是很多。 – Grumpy

+0

你能否提供你的樣本數據和表結構 – Beginner

+0

@Grumpy不幸的是,我必須使用的數據庫有1個帳戶的記錄分裂了超過10個表,由於創建/使用它的程序 – Jordan

回答

1

所以,如果你理解正確,如果你有一個帳戶,有一個對應的數字qs_quotationinformation.takenup的值爲1,那麼對於accounts_cstm.nolongercontact_c AS NLC,查詢應該返回「否」,對於全部記錄具有相同的帳戶ID,而不管qs_quotationinformation.takenup字段在其他記錄中的值。

在這種情況下,您需要獲取qs_quotationinformation.takenup = 1的帳戶列表,並且您可以使用子查詢返回此信息,該信息可以保留加入主查詢。 accounts_cstm.nolongercontact_c AS NLC將更改爲case表達式以基於子查詢返回「No」值。

SELECT 
     accounts.name AS business, 
     accounts.industry AS style, 
     accounts_cstm.renewaldate_c AS ren_date, 
     case 
      when no_nlc.qs_quot108funts_ida is null then accounts_cstm.nolongercontact_c 
      else 'No' 
     end 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 
    LEFT JOIN 
     (SELECT 
      qs_quot108funts_ida 
     FROM 
      qs_quotamation_accounts_c 
     INNER JOIN 
      qs_quotationinformation ON qs_quotamation_accounts_c.qs_quotdb81tion_idb = qs_quotationinformation.id 
     WHERE 
      qs_quotationinformation.takenup = 1) no_nlc ON accounts.id = no_nlc.qs_quot108funts_ida 
    WHERE 
     accounts.deleted = 0 

case表達式假設accounts_cstm.nolongercontact_c字段是字符串類型(CHAR,VARCHAR,等)。如果不是這種情況,那麼您需要使用cast()函數將值accounts_cstm.nolongercontact_c字段賦值爲char。

+0

不,我可能應該使用一個不同的變量到'nolongercontact_c'的AS它應該做的是如果有任何帳戶至少有一次'accounts_cstm.nolongercontact_c'或'qs_quotationinformation.takenup',其值爲1,則變量$ NLC需要設置爲「否」。否則,它需要設置爲「是」 – Jordan

+0

然後,只需更改上面的'case'表達式以返回''是''而不是'accounts_cstm.nolongercontact_c'。 – Shadow