PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Retrieving a list without duplicates
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Retrieving a list without duplicates

Réponse
 
LinkBack Outils de la discussion
Vieux 08/02/2008, 15h26   #1
donpro
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Retrieving a list without duplicates

Hi,

I have a table called "ports" of city names index on the name which is
unique. I have a second table called "jobs" where each job is for a
single city. The relationship is that only some cities can be used as
jobs but more than one job can have the same cities

I'd like to query the "ports" table to retrieve only those cities that
appear in the jobs table so I used the following select query:

select ports.portcode, ports.name from ports
left join jobs on jobs.destinationport=ports.portcode
where ports.portcode=jobs.destinationport order by name

While the above query works, I get duplicate entries of city names
whereas I'd like only one. Any ideas on how to modify the above query
to achieve this? Using MySQL 5.0.18. Thanks in advance.

example of what I get back
--------------------------------------
Aachen
Antwerp
Antwerp
Antwerp
Hong Kong
Singapore
Singapore
Singapore
Singapore

example of what I want
---------------------------------
Aachen
Antwerp
Hong Kong
Singapore

  Réponse avec citation
Vieux 08/02/2008, 15h40   #2
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving a list without duplicates

donpro schreef:
> Hi,
>
> I have a table called "ports" of city names index on the name which is
> unique. I have a second table called "jobs" where each job is for a
> single city. The relationship is that only some cities can be used as
> jobs but more than one job can have the same cities
>
> I'd like to query the "ports" table to retrieve only those cities that
> appear in the jobs table so I used the following select query:
>
> select ports.portcode, ports.name from ports
> left join jobs on jobs.destinationport=ports.portcode
> where ports.portcode=jobs.destinationport order by name
>
> While the above query works, I get duplicate entries of city names
> whereas I'd like only one. Any ideas on how to modify the above query
> to achieve this? Using MySQL 5.0.18. Thanks in advance.
>
> example of what I get back
> --------------------------------------
> Aachen
> Antwerp
> Antwerp
> Antwerp
> Hong Kong
> Singapore
> Singapore
> Singapore
> Singapore
>
> example of what I want
> ---------------------------------
> Aachen
> Antwerp
> Hong Kong
> Singapore
>


use

SELECT distinct .....

http://dev.mysql.com/doc/refman/5.0/...imization.html


  Réponse avec citation
Vieux 08/02/2008, 19h10   #3
strawberry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving a list without duplicates

On 8 Feb, 15:40, Luuk <L...@invalid.lan> wrote:
> donpro schreef:
>
>
>
>
>
> > Hi,

>
> > I have a table called "ports" of city names index on the name which is
> > unique. I have a second table called "jobs" where each job is for a
> > single city. The relationship is that only some cities can be used as
> > jobs but more than one job can have the same cities

>
> > I'd like to query the "ports" table to retrieve only those cities that
> > appear in the jobs table so I used the following select query:

>
> > select ports.portcode, ports.name from ports
> > left join jobs on jobs.destinationport=ports.portcode
> > where ports.portcode=jobs.destinationport order by name

>
> > While the above query works, I get duplicate entries of city names
> > whereas I'd like only one. Any ideas on how to modify the above query
> > to achieve this? Using MySQL 5.0.18. Thanks in advance.

>
> > example of what I get back
> > --------------------------------------
> > Aachen
> > Antwerp
> > Antwerp
> > Antwerp
> > Hong Kong
> > Singapore
> > Singapore
> > Singapore
> > Singapore

>
> > example of what I want
> > ---------------------------------
> > Aachen
> > Antwerp
> > Hong Kong
> > Singapore

>
> use
>
> SELECT distinct .....
>
> http://dev.mysql.com/doc/refman/5.0/...mization.html- Hide quoted text -
>
> - Show quoted text -


or group by ports.name
  Réponse avec citation
Vieux 09/02/2008, 03h38   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving a list without duplicates

strawberry wrote:
> On 8 Feb, 15:40, Luuk <L...@invalid.lan> wrote:
>> donpro schreef:
>>
>>
>>
>>
>>
>>> Hi,
>>> I have a table called "ports" of city names index on the name which is
>>> unique. I have a second table called "jobs" where each job is for a
>>> single city. The relationship is that only some cities can be used as
>>> jobs but more than one job can have the same cities
>>> I'd like to query the "ports" table to retrieve only those cities that
>>> appear in the jobs table so I used the following select query:
>>> select ports.portcode, ports.name from ports
>>> left join jobs on jobs.destinationport=ports.portcode
>>> where ports.portcode=jobs.destinationport order by name
>>> While the above query works, I get duplicate entries of city names
>>> whereas I'd like only one. Any ideas on how to modify the above query
>>> to achieve this? Using MySQL 5.0.18. Thanks in advance.
>>> example of what I get back
>>> --------------------------------------
>>> Aachen
>>> Antwerp
>>> Antwerp
>>> Antwerp
>>> Hong Kong
>>> Singapore
>>> Singapore
>>> Singapore
>>> Singapore
>>> example of what I want
>>> ---------------------------------
>>> Aachen
>>> Antwerp
>>> Hong Kong
>>> Singapore

>> use
>>
>> SELECT distinct .....
>>
>> http://dev.mysql.com/doc/refman/5.0/...mization.html- Hide quoted text -
>>
>> - Show quoted text -

>
> or group by ports.name
>


SELECT DISTINCT is the correct way to go. Grouping is non-standard SQL
and only works with MySQL.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 09/02/2008, 14h29   #5
strawberry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving a list without duplicates

On Feb 9, 3:38 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> strawberry wrote:
> > On 8 Feb, 15:40, Luuk <L...@invalid.lan> wrote:
> >> donpro schreef:

>
> >>> Hi,
> >>> I have a table called "ports" of city names index on the name which is
> >>> unique. I have a second table called "jobs" where each job is for a
> >>> single city. The relationship is that only some cities can be used as
> >>> jobs but more than one job can have the same cities
> >>> I'd like to query the "ports" table to retrieve only those cities that
> >>> appear in the jobs table so I used the following select query:
> >>> select ports.portcode, ports.name from ports
> >>> left join jobs on jobs.destinationport=ports.portcode
> >>> where ports.portcode=jobs.destinationport order by name
> >>> While the above query works, I get duplicate entries of city names
> >>> whereas I'd like only one. Any ideas on how to modify the above query
> >>> to achieve this? Using MySQL 5.0.18. Thanks in advance.
> >>> example of what I get back
> >>> --------------------------------------
> >>> Aachen
> >>> Antwerp
> >>> Antwerp
> >>> Antwerp
> >>> Hong Kong
> >>> Singapore
> >>> Singapore
> >>> Singapore
> >>> Singapore
> >>> example of what I want
> >>> ---------------------------------
> >>> Aachen
> >>> Antwerp
> >>> Hong Kong
> >>> Singapore
> >> use

>
> >> SELECT distinct .....

>
> >>http://dev.mysql.com/doc/refman/5.0/...tion.html-Hide quoted text -

>
> >> - Show quoted text -

>
> > or group by ports.name

>
> SELECT DISTINCT is the correct way to go. Grouping is non-standard SQL
> and only works with MySQL.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================


OK - but we're still left with one question. Seeing as no new
information is gleaned from the (left) join, what's the point of the
OP's sql query?
  Réponse avec citation
Vieux 09/02/2008, 15h42   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving a list without duplicates

strawberry wrote:
> On Feb 9, 3:38 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> strawberry wrote:
>>> On 8 Feb, 15:40, Luuk <L...@invalid.lan> wrote:
>>>> donpro schreef:
>>>>> Hi,
>>>>> I have a table called "ports" of city names index on the name which is
>>>>> unique. I have a second table called "jobs" where each job is for a
>>>>> single city. The relationship is that only some cities can be used as
>>>>> jobs but more than one job can have the same cities
>>>>> I'd like to query the "ports" table to retrieve only those cities that
>>>>> appear in the jobs table so I used the following select query:
>>>>> select ports.portcode, ports.name from ports
>>>>> left join jobs on jobs.destinationport=ports.portcode
>>>>> where ports.portcode=jobs.destinationport order by name
>>>>> While the above query works, I get duplicate entries of city names
>>>>> whereas I'd like only one. Any ideas on how to modify the above query
>>>>> to achieve this? Using MySQL 5.0.18. Thanks in advance.
>>>>> example of what I get back
>>>>> --------------------------------------
>>>>> Aachen
>>>>> Antwerp
>>>>> Antwerp
>>>>> Antwerp
>>>>> Hong Kong
>>>>> Singapore
>>>>> Singapore
>>>>> Singapore
>>>>> Singapore
>>>>> example of what I want
>>>>> ---------------------------------
>>>>> Aachen
>>>>> Antwerp
>>>>> Hong Kong
>>>>> Singapore
>>>> use
>>>> SELECT distinct .....
>>>> http://dev.mysql.com/doc/refman/5.0/...tion.html-Hide quoted text -
>>>> - Show quoted text -
>>> or group by ports.name

>> SELECT DISTINCT is the correct way to go. Grouping is non-standard SQL
>> and only works with MySQL.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================

>
> OK - but we're still left with one question. Seeing as no new
> information is gleaned from the (left) join, what's the point of the
> OP's sql query?
>


It's supposed to show a list of city names (from the ports table) which
have at least one entry in the jobs table.

I don't think he'll want a LEFT JOIN in this case. But one problem at a
time.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19900 seconds with 14 queries