473,670 Members | 2,550 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_arr ay($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 1672
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_arr ay($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*******@attgl obal.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("S ELECT 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
3792
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, searching for values a user inputs via a Search form. I have a solution, but I don't think it'll work on large scales. Is there a better bet then querying table 1, finding matching values and writing those records into an ADO variable, closing...
3
12883
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 SURNAME
0
1841
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 and link the 2 tables, so that the first record of A is associated with the fist record of B, the second record of A is associated (I mean,in the same row) with the second record of B and so on...until the last row of A (where x<y). I tried to...
0
1267
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 attendance occurance. I would like to create a report that will total each type of absence in its own column. Can anyone help me with this?
0
1259
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
5551
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
2247
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 suggested I redesigned my database to use more tables of info. Before I had 1 table for image information, now I have 9 and it's getting very, very confusing... especially as I don't know much about joins...! I am using ASP, MySQL, IIS and VBScript. ...
1
3406
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 Costed and Invoiced data from the same database. I've tried to use a JOIN but it keeps failing. I've got multiple fields that needs to be linked. Any suggestions. I've attached the query to make it easier Declare @IDateFrom DateTime, ...
13
9055
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 some information that is encoded in, shall we say, a non-intuitive way. And that encoding has changed over the years. One of my users noticed today that a report she ran produced an unexpected result. Tracking it down, I found that different queries...
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7421
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6216
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.