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

unifying two or more sql query results

shane3341436
I need to unite two or more sql query results into one single array of results.
The two tables have unequal number of columns. I tried to do this by
for an example:
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT P_id id,P_fname name,P_lname lname,P_address address FROM person";
  2. $sql .=" union ";
  3. $sql .="SELECT C_id id,C_name name,C_name lname,C_address address FROM company";
  4.  
Here I used C_name twice just to fit within union. If I remove the second one the sql doesn't work.
My problem is what if the first table has many columns ,say 10 and the second table has only 4 or 5 columns. I have to repeat one of the columns just to fit the 'union'. Is there any other ways to unite them
Sep 7 '07 #1
3 2256
Atli
5,058 Expert 4TB
Hi Shane.

the UNION syntax expects that the SELECT statements return the same amount of columns. There is no way to bypass that.

You could of course use the CONCAT() function to splice together the first and last names in the person table, if that works for you.

Why do you want to unify those results? Why not just use two separate queries?
Sep 7 '07 #2
thanx for the response.
I wanted to unify those results so that I could use the result in single while loop. Otherwise I must have to repeat the while loops many times if there are many tables. The above query is only the example
Sep 10 '07 #3
pbmods
5,821 Expert 4TB
Heya, Shane.

Sounds like you might want to rethink your database design.

You'd have no good way to determine when you had hit the end of a table in your result set if you used a UNION anyway.
Sep 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my...
2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
2
by: WTH | last post by:
....and I'd like to write it in such a way that I do not prevent us from using C# in the near future (as I think the language is great.) The caveats are that it must be written in unmanaged C++....
12
by: koorb | last post by:
Is there any reason why Microsoft is keeping the languages separate or will they eventual replace all the languages with one, just to simplify? And why do people keep talking about switching to...
0
by: Gordon.E.Anderson | last post by:
short description: i've got a .net web site set up using a tableadapter attached to a sql server table - returning results only, no update of data. i've got a query (qry code below) set up to...
2
by: Eric S. Johansson | last post by:
I have a collection of packages and I want to put them under single unifying name. my goal is to reduce namespace pollution and make all these packages accessible as 'import vvv.aaa'. In more...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
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
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
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...
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.