从两个表中选择数据,mysql

时间:2014-04-03 09:17:27

标签: mysql

我的数据库有两个表客户和交易。

客户表有8列,事务表有4列,

客户表主键是id,而在事务表中没有主键,每个客户在事务表中都存在多个记录。

我需要从客户中选择4列,从事务中选择3列。

这里是查询:

query ='SELECT c.customer_id AS "Customer ID",
               c.father_name AS "Father Name",
               c.address AS Address,
               c.phone_number AS "Phone Number",
               c.mobile_number AS "Mobile Number",
               c.id_proof AS "ID Proof",
               c.area AS "Area",
               c.ip_address AS "IP Address",
               c.mac_address AS "MAC Address",
               c.package_type AS "Package Type",
               c.name AS Name,
               c.activation_date AS "Activation Date",
               c.status AS "Status",
               c.installation_cost AS "Installation Cost",
               c.totalamount_paid AS "Total Amount Paid",
               c.monthly_amount AS "Monthly Amount",
               c.lastpaid_date AS "Last Paid Date",
               c.lastpaid_amount AS "Last Paid Amount",
               c.nextpay_date AS "Next Pay Date",
               c.totaldue_amount AS "Total Due Amount",
               t.agent_id AS "Agent ID",
               t.token_number AS "Token Number",
               t.machine_id AS "Machine ID"
          FROM customer c INNER JOIN transaction t ON c.customer_id = t.customer_id
        WHERE DATE(t.paid_date)BETWEEN '" + fromDate + "'AND '" + toDate + "' ";

但问题是:

记录在事务表中存在多少次,那些记录存在的次数,但我只需要一次。

我该怎么做?

4 个答案:

答案 0 :(得分:1)

... GROUP BY c.customer_id

您很可能希望 SUM 某些列或使用其他聚合函数。

答案 1 :(得分:1)

问题在于您的查询逻辑。您从transaction表中选择了三列。现在,对于单个客户,可以有多个交易。如果您选择t.agent_idt.token_numbert.machine_id,则会为客户的每笔交易提供这些值。

如果您的业务逻辑允许这样做,请从select列表中删除这三列,并使用EXISTS子句代替INNER JOIN,如下所示:

query ='SELECT c.customer_id AS "Customer ID",
               c.father_name AS "Father Name",
               c.address AS Address,
               c.phone_number AS "Phone Number",
               c.mobile_number AS "Mobile Number",
               c.id_proof AS "ID Proof",
               c.area AS "Area",
               c.ip_address AS "IP Address",
               c.mac_address AS "MAC Address",
               c.package_type AS "Package Type",
               c.name AS Name,
               c.activation_date AS "Activation Date",
               c.status AS "Status",
               c.installation_cost AS "Installation Cost",
               c.totalamount_paid AS "Total Amount Paid",
               c.monthly_amount AS "Monthly Amount",
               c.lastpaid_date AS "Last Paid Date",
               c.lastpaid_amount AS "Last Paid Amount",
               c.nextpay_date AS "Next Pay Date",
               c.totaldue_amount AS "Total Due Amount" /*,
               t.agent_id AS "Agent ID",
               t.token_number AS "Token Number",
               t.machine_id AS "Machine ID" -- */
          FROM customer c 
        WHERE EXISTS (SELECT 1
                        FROM transaction t
                       WHERE c.customer_id = t.customer_id
                         AND DATE(t.paid_date)BETWEEN '"
                                                        + fromDate
                                                        + "'AND '"
                                                        + toDate + "' "

现在,如果您的商家确实需要这三个值,您只需使用SELECT DISTINCT并停止担心重复值。

答案 2 :(得分:0)

试试这个 -

query = " ... ... ... GROUP BY c.customer_id";

答案 3 :(得分:0)

query ='SELECT c.customer_id AS "Customer ID", c.father_name AS "Father Name", c.address AS Address, c.phone_number AS "Phone Number", c.mobile_number AS "Mobile Number", c.id_proof AS "ID Proof", c.area AS "Area", c.ip_address AS "IP Address", c.mac_address AS "MAC Address", c.package_type AS "Package Type", c.name AS Name, c.activation_date AS "Activation Date", c.status AS "Status", c.installation_cost AS "Installation Cost", c.totalamount_paid AS "Total Amount Paid", c.monthly_amount AS "Monthly Amount", c.lastpaid_date AS "Last Paid Date", c.lastpaid_amount AS "Last Paid Amount", c.nextpay_date AS "Next Pay Date", c.totaldue_amount AS "Total Due Amount", t.agent_id AS "Agent ID", t.token_number AS "Token Number", t.machine_id AS "Machine ID" FROM customer c INNER JOIN transaction t ON c.customer_id = t.customer_id WHERE DATE(t.paid_date)BETWEEN '" + fromDate + "'AND '" + toDate + "' " GROUP BY c.customer_id;