473,404 Members | 2,114 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,404 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 1659
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...
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: 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
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.