普通网友 2010-04-22 20:24
浏览 46
已采纳

在mysql查询中摆脱重复的客户ID

I originally started by selecting customers from a group of customers and then for each customer querying the records for the past few days and presenting them in a table row. All working fine but I think I might have got too ambitious as I tried to pull in all the records at once having heard that mutiple queries are a big no no.

here is the mysqlquery i came up with to pull in all the records at once

SELECT morning, afternoon, date, date2, fname, lname,  customers.customerid
FROM customers
LEFT OUTER JOIN attend ON ( customers.customerid = attend.customerid ) 
RIGHT OUTER JOIN noattend ON ( noattend.date2 = attend.date ) 
WHERE noattend.date2
BETWEEN '$date2'
AND '$date3'
AND DayOfWeek( date2 ) %7 >1
AND group ={$_GET['group']}
ORDER BY lname ASC , fname ASC , date2 DESC 

tables are customer->customerid,fname,lname

attend->customerid,morning,afternoon,date

noattend->date2 (a table of all the days to fill in the blanks)

Now the problem I have is how to start a new row in the table when the customer id changes My query above pulls in

customer 1 morning 2

customer 1 morning 1

customer 2 morning 2

customer 2 morning 1

whereas I'm trying to get

customer1 morning2 morning1

customer2 morning2 morning1

I dont know whether this is possible in the sql or more likely in the php

  • 写回答

4条回答 默认 最新

  • doutui8842 2010-05-03 22:23
    关注

    I finally worked out what I was missing.

    In order to address the element of the array I needed to use, I needed to use a double bracket ie $customer_array[0][lname], $customer_array[1][lname]. I realise this is probably obvious to most but it was completely eluding me. The key to my understanding this was
    print_r(customer_array) which I'd seen a lot but never got working properly.

    Then it was just a case of pulling out all the database rows with:

    $customer_array =array();
    while($row1=mysql_fetch_assoc($extract1)){
    $customer_array[] = $row1; 
    }
    

    and then to loop through as I have a fixed number of records:

     for ($x=0;$x<=900;)
    { 
    echo $customer_array[$x][fname];
    echo$customer_array[$x][lname];
    for($y=0;$y<=30;$y++)
    {
    echo $customer_array[$x][morning];
    echo $customer_array[$x][afternoon];
            $x++;
        }
         }
    

    Hope this helps someone else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)