473,394 Members | 1,854 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,394 software developers and data experts.

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 help will be greatly appreciated.
Mar 22 '06 #1
2 2724
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 help 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)

Mar 22 '06 #2
noone <no***@nowhere.com> wrote in
news:c0********************************@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.
Mar 23 '06 #3

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

Similar topics

4
by: David Walker | last post by:
Hi again As I'm developing my site, i've come across a problem with the database use. While developing it I only have mySQL, but the final server has msSQL. I was thinking about using all my own...
5
by: Chumley the Walrus | last post by:
When i put an explicit value in the sql statement below as "displaygroup =1 ", i do see my records (or my response.write test after the recordset Open line) appear on my dynamic page. But when use...
17
by: Gabriel Mejía | last post by:
Services or applications using ActiveX Data Objects (ADO) 2.0 or greater may intermittently return empty recordsets on queries that should be returning valid results. At the time the problem...
4
by: dysfunctional | last post by:
Hi there, I'm a complete zero at javascript in need of a hero. I run a free, non-commercial site which includes a searchable database of hotels in Morocco. Find the page at...
5
by: Sim Zacks | last post by:
I just did a dump and restore of my database and one of my views did not recreate. The error received was : pg_restore.exe: could not execute query: ERROR: column reference "pricinggroupid" is...
2
by: Bob Sanderson | last post by:
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...
1
by: DonWolfi | last post by:
I am trying to get of our the database of our finance system all records where the the first field is the same and that either comply with condition a or condition b. Example: /<font...
1
by: | last post by:
I'm querying Index Server to return search results, both regular properties and some custom properties I've created. Index Server has this preference for thinking about information as strings...
7
by: ruvi | last post by:
I am getting runtime error 3021 - Either EOF or BOF is true or the current record has been deleted..... I have 2 combo boxes in a form- One for the client and the other for the project. When the...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.