2016-09-29 1 views
0

MYSQL 관계에 약간의 문제가 있습니다. 표 1의 모든 1 개 값이관련된 모든 행에 문자열이 포함 된 MYSQL 및 PHP

는, 내가 제대로 모든 데이터를 얻을 수 있어요 2.

은, 그러나, 문제는 때 몇 가지를 제공 테이블 값 (0+)의 군중이있을 수있다 표 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은 "accounts"이고 표 2는 "qs_quotationinformation"입니다.

이 표 2에 여러 레코드가 표 1의 일부 계정이 있고, (동일한 계정에 관한) 표 2에서 "takenup"기록 중 일부는 하나가 될 수 있으며, 일부는 0

그래서 수 내가해야 할 일은 표 2의 레코드 중 하나라도 1이면 $ NLC의 모든 변수가 = "아니오"일 필요가 있습니다.

이것이 가능한지 또는이 정보를 얻는 더 좋은 방법이 있는지 나는 모른다. html 테이블에는 데이터가 누락되어 있지만 표는 사용자에게 가장 중요한 데이터를 시각적으로 표시하기위한 것입니다. (개인 데이터는 제외)

EDIT 테이블 :

표 1 : +----+---------+---------+ | id | name | deleted | +----+---------+---------+ | 1 | example | 0 | +----+---------+---------+

표 2 : +----+---------+ | id | takenup | +----+---------+ | 1 | 0 | +----+---------+ | 2 | 1 | +----+---------+ | 3 | 0 | +----+---------+

표 2의 모든 행은 표 1에 때문이 아니라, 행과 관련이 takenup = 1 $ NLC가 "아니오"를 반환해야하고 "예"가 아닌 행을 반환해야합니다 (마지막 관련 행이 0이기 때문에 현재 수행 중임)

+0

그냥 팁을 최대한 줄 이도록 조인을 시도하십시오. 7 많이 있습니다. – Grumpy

+0

샘플 데이터와 테이블 구조도 제공 할 수 있습니다. – Beginner

+0

불행히도 불행히도 제가 작업해야하는 데이터베이스는 생성/사용하는 프로그램으로 인해 10 개의 테이블에서 하나의 계정 레코드가 나뉘어져 있습니다. – Jordan

답변

1

계정을 갖고 계시다면 , 그것은 상응하는 ponding qs_quotationinformation.takenup 값이 1이면 쿼리는 다른 레코드의 qs_quotationinformation.takenup 필드에 관계없이 에 대해 accounts_cstm.nolongercontact_c AS NLC에 대해 모두 No 레코드에 대해 "No"를 반환해야합니다.

이 경우 qs_quotationinformation.takenup = 1 인 계정 목록을 가져와야하며 하위 쿼리를 사용하여이 정보를 반환 할 수 있습니다.이 정보는 기본 쿼리에 가입 할 수 있습니다. accounts_cstm.nolongercontact_c AS NLCcase 표현식으로 변경되어 하위 쿼리를 기반으로 "아니요"값을 반환합니다.

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를 "No"로 설정해야합니다.그렇지 않으면 "Yes"로 설정해야합니다. – Jordan

+0

위의 case 식을 변경하여 accounts_cstm.nolongercontact_c 대신에 'Yes'를 반환하십시오. – Shadow

관련 문제