473,513 Members | 11,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Joining search queries

Newbie question.

Its really an SQL question, but have had no reply from the SQL
newsgroup.

I want to search the same table for two different criteria, and then
join the search results together into one new list. See below.

<?php

$result = @mysql_query ("SELECT company, priority FROM table1 WHERE
priority ='high' "); UNION; @mysql_query ("SELECT company, priority
FROM table1 WHERE priority ='low' ");

while ($row = mysql_fetch_array($result))

{

$company = $row['company']; echo $company;
$priority = $row['priority']; echo $priority;

}
?>

BUT it only returns the first search result, even though there are
records that relate to the second.

What am I doing wrong? Also can UNION be used to combine more than two
searches?

Many thanks

Alec

Oct 8 '07 #1
2 1657
Alec wrote:
Newbie question.

Its really an SQL question, but have had no reply from the SQL
newsgroup.

I want to search the same table for two different criteria, and then
join the search results together into one new list. See below.

<?php

$result = @mysql_query ("SELECT company, priority FROM table1 WHERE
priority ='high' "); UNION; @mysql_query ("SELECT company, priority
FROM table1 WHERE priority ='low' ");

while ($row = mysql_fetch_array($result))

{

$company = $row['company']; echo $company;
$priority = $row['priority']; echo $priority;

}
?>

BUT it only returns the first search result, even though there are
records that relate to the second.

What am I doing wrong? Also can UNION be used to combine more than two
searches?

Many thanks

Alec
Where did you ask? I didn't see it in comp.databases.mysql, which is
where you should be asking.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 8 '07 #2
Alec wrote:
$result = @mysql_query ("SELECT company, priority FROM table1 WHERE
priority ='high' "); UNION; @mysql_query ("SELECT company, priority
FROM table1 WHERE priority ='low' ");
First thing to remember: post readable code.

$result = @mysql_query("SELECT company, priority FROM table1
WHERE priority ='high' ");
UNION;
@mysql_query ("SELECT company, priority FROM table1
WHERE priority ='low' ");

Now it is quite visible what is happening. You only assign the
first query to $result.

Solution if the only priorities that exist are "high" and "low":

SELECT company, priority FROM table1;

Other solution:

SELECT company, priority FROM table 1
WHERE priority in ('high', 'low');

The less-than-optimal, but correct UNION-solution:

SELECT company, priority FROM table 1
WHERE priority ='high')
UNION
SELECT company, priority FROM table 1
WHERE priority = 'low');

You REALLY should read some SQL-Tutorial, because this is
really BASIC knowledge about SQL.

Oct 9 '07 #3

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

Similar topics

11
3781
by: Adrian Parker | last post by:
I have a database of 200+ tables (two tables per school), each with 100 - 4000 records (one record per student). A contract I'm looking at wants to be able to do a search across all the tables,...
3
12876
by: Matt O'Donnell | last post by:
Does anyone know how I can 'join' the results of one SQL query to the bottom of another? Eg. I have two queries: 1. SELECT Name, Surname FROM People WHERE Surname = Smith NAME ...
0
1836
by: Gianfranco | last post by:
Hi I got a problem with 2 tables. I have a table, say A, with x records, coming from a make table query and a table, say B, with y records, coming from another make table query. I need to join...
0
1259
by: Norma | last post by:
I have 2 queries that pull attendance records depending on the type of attendance occurance, or Both queries are drawn off 1 table where the field will depict whether it is a punctuality or...
0
1252
by: dkintheuk | last post by:
Hi all, I'm trying to output a result set from some data as follows: I have raw data with the following columns Date, Country, System, ID,
12
5536
by: veaux | last post by:
Question about joins in queries. I have 2 tables with a field called "ID". Table 1 Rec1 = Jan12FredFlintstone Rec2 = Feb01WilmaRubble Table 2 Rec1 = Jan12BarneyRubble Rec2 = Mar03SamSlate
10
2233
by: marting | last post by:
Before I throw my new expensive laptop out of the window! I'm stuck on getting my joins right with the correct amount in a column count. After speaking with someone a few weeks back, they...
1
3401
by: bhavinnaik | last post by:
Hi I am new to the IT enviro...although i've used QSL query for a while now but on simple or single queries. Here is the problem... I've got two queries looking at a set of tables to extract the...
13
9039
prn
by: prn | last post by:
Hi folks, I have an inherited access application. At least a half-dozen people have worked on this one before me. The application contains hundreds of queries. My (current) problem involves...
0
7254
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
7153
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
7373
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,...
1
7094
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
5677
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,...
1
5079
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...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.