dqask02082 2013-04-17 09:09
浏览 47
已采纳

如何从MySQL表中选择列1和列2值唯一的所有数据[关闭]

I am new to php and MySql and am trying to write a php script to query a MySQL table. I have spent hours searching stackoverflow but don't quite understand some of the answers from similar questions like How to select unique values from a table? and MYSQL + Select 2 columns - 1 being Unique

I have a table with a 'location' column and an 'address' column and some other columns with other information about the location etc. The location is a name of, for example, a shop. The address column holds the street address of that shop.

A shop sometimes has multiple outlets at different addresses and I need to have a query that shows all data from the table with the location only being displayed multiple times if the address is unique.

I have the php connection to the database and selection of the database working but need help with the MySql query.

I have used a GROUP BY function on the location, but all addresses get put under the same location.

This is what I have used but only unique locations get displayed.

<?php
mysql_connect("localhost","username","password");
mysql_select_db("dbname");
$sql=mysql_query("SELECT * from table GROUP BY location");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

How do I go about making a query the allows the location to be displayed multiple times if the address is unique?

Thanking you in advance..

Not sure how to post table structure but this is what I could get from myphpadmin

**Column**      **Type**        **NULL**        **Comments**
_id              int(11)           No       auto_increment 
location         text              No       Shop Name 
address          text              No       Shop Address 
suburb           varchar(30)       No        
state            varchar(3)        No        
longitude        float             No       longitude 
latitude         float             No       latitude 
date             date              No        
time             time              No        

What I am after is all locations with a unique address. Some locations will have the same name and same address, but some locations will have the same name but a different address. For example.. If table looked like

**Location**     **Address**
Target           Pacific Fair
Kmart            Pacific Fair
Kmart            Robina
Kmart            Southport
Target           Pacific Fair
Target           Tweed City
Kmart            Pacific Fair
Best and Less    Pacific Fair

I want the results to show

**Location**     **Address**
Target           Pacific Fair
Target           Tweed City
Kmart            Pacific Fair
Kmart            Robina
Kmart            Southport
Best and Less    Pacific Fair
  • 写回答

2条回答 默认 最新

  • dongnuo4594 2013-04-17 11:26
    关注

    If I understand your question correctly, you need to group by both location and address:

    SELECT location, address FROM table GROUP BY location, address
    

    or you can use DISTINCT:

    SELECT DISTINCT location, address FROM table
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line