PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > Can I use an either/or query?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Can I use an either/or query?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/03/2006, 20h55   #1 (permalink)
Bob Sanderson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Can I use an either/or query?

I am using the following query to generate a web page. Searchterm is
derived from a search form.

$query="select * from jobs, items where jobs.JobNumber like '$Searchterm'
and items.JobNumber like '$Searchterm'";

The data from the jobs table goes in a general form describing a specific
job. The data from the items table goes in a separate form which lists all
of the items associated with that job. This works fine if there is data for
the selected job number in both tables but in some cases, the data only
exists in the jobs table - there is no corresponding data in the items
table. What I would like in that case is to output the jobs table data and
simply leave the items output blank, but since there are no records meeting
the criteria of the query, nothing is selected.

Is there a way to create a query so that it will do what I want. If not,
can it be done with an either/or statement?

Any will be greatly appreciated.
  Réponse avec citation
Vieux 22/03/2006, 21h48   #2 (permalink)
noone
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can I use an either/or query?

Bob Sanderson wrote:

> I am using the following query to generate a web page. Searchterm is
> derived from a search form.


> $query="select * from jobs, items where jobs.JobNumber like '$Searchterm'
> and items.JobNumber like '$Searchterm'";


> The data from the jobs table goes in a general form describing a specific
> job. The data from the items table goes in a separate form which lists all
> of the items associated with that job. This works fine if there is data for
> the selected job number in both tables but in some cases, the data only
> exists in the jobs table - there is no corresponding data in the items
> table. What I would like in that case is to output the jobs table data and
> simply leave the items output blank, but since there are no records meeting
> the criteria of the query, nothing is selected.


> Is there a way to create a query so that it will do what I want. If not,
> can it be done with an either/or statement?


> Any will be greatly appreciated.


A general rule is to explicitly specify each column in each field. One
method is a Left Outer Join.


select a.id, a.data1,a.data2,b.data1,b.data2 from
tablea a left outer join tableb b on a.id=b.id where a.id like
('$searchterm')

b.data1 and b.data2 will be NULL if there is no data from items table.


Example:

mysql> select * from c;
+------+
| a |
+------+
| 2 |
+------+
1 row in set (0.01 sec)

mysql> select * from d;
+------+----------------+
| a | b |
+------+----------------+
| 1 | 20060313165232 |
| 1 | 20060313155236 |
| 1 | 20060314215241 |
| 1 | 20060313145251 |
| 2 | 20060321060235 |
| 2 | 20060322020243 |
| 3 | 20060322020254 |
| 3 | 20060322080300 |
| 3 | 20060322100305 |
+------+----------------+
9 rows in set (0.01 sec)

mysql> select d.a,d.b,c.a from d left outer join c on c.a=d.a;
+------+----------------+------+
| a | b | a |
+------+----------------+------+
| 1 | 20060313165232 | NULL |
| 1 | 20060313155236 | NULL |
| 1 | 20060314215241 | NULL |
| 1 | 20060313145251 | NULL |
| 2 | 20060321060235 | 2 |
| 2 | 20060322020243 | 2 |
| 3 | 20060322020254 | NULL |
| 3 | 20060322080300 | NULL |
| 3 | 20060322100305 | NULL |
+------+----------------+------+
9 rows in set (0.01 sec)



  Réponse avec citation
Vieux 23/03/2006, 14h22   #3 (permalink)
Bob Sanderson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Can I use an either/or query?

noone <noone@nowhere.com> wrote in
news:c0be99a872d26915da7bafc7d45c7ae6$1@somehostou tintheEther.com:

> A general rule is to explicitly specify each column in each field.
> One method is a Left Outer Join.
>
>
> select a.id, a.data1,a.data2,b.data1,b.data2 from
> tablea a left outer join tableb b on a.id=b.id where a.id like
> ('$searchterm')
>
> b.data1 and b.data2 will be NULL if there is no data from items table.


Works great, thanks.
  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 01h05.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08335 seconds with 11 queries