473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Use of Distinct ?

ben
This is a PHP / MySQL kind of question. I am making a script which
simply pulls information from a database and displays it on screen. BUT
there will be entries where fields could be the same, and I want to
eliminate the duplicate entries when it displays it on screen.

I would use,

SELECT DISTINCT 'address1' FROM orders

....which does work, but only gives me one field. I also need to get a
few other fields such as name and address1. I cant think of a way to
eliminate the duplicates and still show all fields.

Does anyone know a way round this ?

ben

Jul 17 '05 #1
3 2155
LEH

"ben" <le*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
This is a PHP / MySQL kind of question. I am making a script which
simply pulls information from a database and displays it on screen. BUT
there will be entries where fields could be the same, and I want to
eliminate the duplicate entries when it displays it on screen.

I would use,

SELECT DISTINCT 'address1' FROM orders

...which does work, but only gives me one field. I also need to get a
few other fields such as name and address1. I cant think of a way to
eliminate the duplicates and still show all fields.

Does anyone know a way round this ?

ben

First a disclaimer : I'm selftaught and there maybe better ways, but this is
the way I have done this.

You can run a series of SQL statements, first selecting the distinct field,
then selecting other records/fields with this distinct field.

But I am assuming that several fields are distinct, or at least the
combination of those fields is distinct. If this is the case, I generally
use the concat function to pull distinct combinations. I will usually also
put in a separator so that i can split afterwards

SELECT DISTINCT CONCAT(address1,"/",name,"/",phone) FROM orders;

then i can splilt the result on / into the appropriate variables.

perhaps at a SQL group you can get a more eloquent answer.

Larry
Jul 17 '05 #2
On 2005-02-27, LEH <l.*******@utoronto.ca> wrote:

"ben" <le*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
This is a PHP / MySQL kind of question. I am making a script which
simply pulls information from a database and displays it on screen. BUT
there will be entries where fields could be the same, and I want to
eliminate the duplicate entries when it displays it on screen.

I would use,

SELECT DISTINCT 'address1' FROM orders

...which does work, but only gives me one field. I also need to get a
few other fields such as name and address1. I cant think of a way to
eliminate the duplicates and still show all fields.

SELECT DISTINCT('address'),other,thing FROM orders
should work

Shamrock
Jul 17 '05 #3
ben
dammit ! lol, that last post did the trick

SELECT DISTINCT('address'),other,thing FROM orders

I ended up going with,

$result = mysql_query("SELECT DISTINCT address1 FROM orders WHERE
approved = '2'");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if(!isset($total)) { $total = ''; }
$i = "0";
$address = $row["address1"];

$result2 = mysql_query("SELECT * FROM orders WHERE address1 =
'$address'");
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$address1 = $row["address1"];
$id = $row2["ID"];
$ordertime = $row2["ordertime"];
$title = $row2["title"];
$name = $row2["name"];
$price = $row2["price"];
$total = $total+$price;
$i++;
}

which now seems overly complex and time consuming ! i think i might
just change it.

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin Feuersteiner | last post by:
Dear Group I'm having trouble with the clause below. I would like to select only records with a distinct TransactionDate but somehow it still lists duplicates. I need to select the...
2
by: mfyahya | last post by:
I have two tables, both containing an 'authors' column. Is there a way to get a unique list of authors from the two tables? I tried SELECT DISTINCT `authors` from `table1`, `table2`; but I got an...
4
by: Johnson, Shaunn | last post by:
Howdy: Can someone tell what the difference (and why you would use it) is between the following: select distinct on (col_1, col_2), col_1, col_2, col_3
18
by: mathilda | last post by:
My boss has been adamant that SELECT DISTINCT is a faster query than SELECT all other factors being equal. I disagree. We are linking an Access front end to a SQL Server back end and normally are...
1
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT...
3
by: orekinbck | last post by:
Hi There Our test database has duplicate data: COMPANYID COMPANYNAME 1 Grupple Group 2 Grupple Group 5 Grupple Group 3 Yada Inc 4 Yada...
6
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214,...
4
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
2
by: Techhead | last post by:
I need to run a SELECT DISTINCT query across multiple fields, but I need to add another field that is NON-DISTINCT to my record set. Here is my query: SELECT DISTINCT lastname, firstname,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.