douqiaoru2583 2016-09-29 08:46
浏览 18
已采纳

MYSQL和PHP,其中任何相关的行包含字符串

I am having a small issue with MYSQL relations.

There is for every 1 value in table 1, there can be a multitude of values (0+) in table 2.

I am able to get all the data correctly, however, the issue comes when some values in table 2 differ, specifically the "taken up" field.

$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";
}

In this case Table 1 is "accounts" and Table 2 is "qs_quotationinformation".

There are some accounts in Table 1 which have multiple records in Table 2, and some of the "takenup" records in Table 2 (relating to the same account) can be 1 and some be 0.

So what I need to do is have it so that if any of the records in Table 2 = 1, then all of the variables of $NLC need to = "No".

I don't know if this is possible, or if there is a better way to get this information. The html table is missing data that gets pulled, but that's because the table is just a visual representation of the most important data to the user.

EDIT Tables (excluding personal data):

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

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

All of the rows in Table 2 relate to the row in Table 1. But because there is a row with takenup = 1 $NLC needs to return "No" and not "Yes" (which it currently does because the last related row is 0)

  • 写回答

1条回答 默认 最新

  • duanqinqian5299 2016-09-29 09:24
    关注

    So, if you understand you correctly, if you have an account, that has a corresponding qs_quotationinformation.takenup value of 1, then the query should return "No" for accounts_cstm.nolongercontact_c AS NLC for all records with the same account id, regardless of the value of qs_quotationinformation.takenup field in the other records.

    In this case you need to get the list of accounts that have qs_quotationinformation.takenup=1 and you can use a subquery to return this information, which can be left joined to the main query. accounts_cstm.nolongercontact_c AS NLC would be changed to a case expression to return the "No" value based on the subquery.

        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
    

    The case expression assumes that accounts_cstm.nolongercontact_c field is of a string type (char, varchar, etc). If this is not the case, then you need to cast the value of accounts_cstm.nolongercontact_c field to char using the cast() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值