473,394 Members | 1,706 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.

Mix two tables in a single SELECT statement - not a join

Hi,

I have an Intranet with the organisation I work with. In this
organisation there are two departments - community department and press
office. Both the community department and press office have thier own
news system where they can add news in their own admin section that
will appear on their homepages. Both admin section have their own DB
table thus two seperate tables.

I would like to select all the rows from both tables in a single SELECT
statement so when I output them they will be integrated. This is not
the same as a table join as their are no keys and I want the rows from
both to remain seperate. Can this be done? Could someone please post an
example SQL statement demonstrating this if so?

Thanks

Burnsy

Nov 7 '05 #1
7 9731
bizt said the following on 07/11/2005 11:24:

I would like to select all the rows from both tables in a single SELECT
statement so when I output them they will be integrated. This is not
the same as a table join as their are no keys and I want the rows from
both to remain seperate. Can this be done? Could someone please post an
example SQL statement demonstrating this if so?


UNION

--
Oli
Nov 7 '05 #2
bizt wrote:
Hi,

I have an Intranet with the organisation I work with. In this
organisation there are two departments - community department and press
office. Both the community department and press office have thier own
news system where they can add news in their own admin section that
will appear on their homepages. Both admin section have their own DB
table thus two seperate tables.

I would like to select all the rows from both tables in a single SELECT
statement so when I output them they will be integrated. This is not
the same as a table join as their are no keys and I want the rows from
both to remain seperate. Can this be done? Could someone please post an
example SQL statement demonstrating this if so?

Thanks

Burnsy

Hi Burnsy,

Do you want to use two different datasources in 1 single query?
I think you are making things overcomplicated.

Why don't you query both datasources seperately, and then in PHP decide
which one to show in your unified 'resultset' ?

Regards,
Erwin Moller
Nov 7 '05 #3
I would like to select all the rows from both tables in a single SELECT
statement so when I output them they will be integrated. This is not
the same as a table join as their are no keys and I want the rows from
both to remain seperate. Can this be done? Could someone please post an
example SQL statement demonstrating this if so?


You need a UNION statement or the equivalent. The syntax will depend on
what database server you are using. I'll assume MySQL, and since UNION
is only available from 4.0.0 onwards I'll assume you have that version.
The main requirement is that the columns of both tables have the same
names and the same types. You haven't given any DDL for the tables so
I'll make some up...

CREATE TABLE CommunityNews ( cnid INT, newsdate DATE, newstext
VARCHAR(1000) )
CREATE TABLE PressNews ( pnid INT, newsdate DATE, newstext
VARCHAR(1000) )

(SELECT newsdate, newstext, "1" AS source
FROM CommunityNews
WHERE newsdate >= '"20051107")
UNION
(SELECT newsdate, newstext, "2" AS source
FROM PressNews
WHERE newsdate >= "20051107")
ORDER BY newsdate DESC

Your result is now a mixture of rows from both tables. You can tell
which table a row came from by looking at the "source" column.

---
Steve

Nov 7 '05 #4
> Why don't you query both datasources seperately, and then in PHP decide
which one to show in your unified 'resultset' ?


Thanks for the suggestion but as well as querying tables one after the
other, wouldnt this also restrict me to outputing them one after the
other. I kinda need to integrate them and then probably order them by
date.

Anyway, I see I need to gain a little understanding of the UNION
command in SQL which is fine. I have tried visiting a couple of
websites and they suggest all selected columns need to be the same data
type. This presents two problems for me:

1) Some columns have different names. For example, both have a text
data type column containing the body text of each article but one is
called 'desctext' and the other 'article'. Could this be worked around
by using the AS command (ie. article AS desctext)?

2) The column that provides the entry date, 'createdon', there are two
data types between the tables. One is datetime (in the format
'2005-11-08 12:04:43' and the other timestamp ('2005-11-08
12:04:43+01'). Is this an issue?

Unfortunately both tables were created a year apart and when I started
working on the later table, this idea wasnt thought of until now.

Burnsy

Nov 8 '05 #5
bizt wrote:
Why don't you query both datasources seperately, and then in PHP decide
which one to show in your unified 'resultset' ?

Thanks for the suggestion but as well as querying tables one after the
other, wouldnt this also restrict me to outputing them one after the
other. I kinda need to integrate them and then probably order them by
date.

Anyway, I see I need to gain a little understanding of the UNION
command in SQL which is fine. I have tried visiting a couple of
websites and they suggest all selected columns need to be the same data
type. This presents two problems for me:

1) Some columns have different names. For example, both have a text
data type column containing the body text of each article but one is
called 'desctext' and the other 'article'. Could this be worked around
by using the AS command (ie. article AS desctext)?


This may be database dependent -- but I know from experience
that if you are using Oracle different column names are not a
problem.
2) The column that provides the entry date, 'createdon', there are two
data types between the tables. One is datetime (in the format
'2005-11-08 12:04:43' and the other timestamp ('2005-11-08
12:04:43+01'). Is this an issue?
Try it and see if it works. If it doesn't would it be feasible
to convert the datetime column to a timestamp?
Unfortunately both tables were created a year apart and when I started
working on the later table, this idea wasnt thought of until now.

Burnsy

Nov 8 '05 #6
Ok, tried a simple example:

$select = "SELECT title, desctext FROM commnews UNION SELECT title,
article AS desctext FROM pressnews ORDER BY createdon";

$result = pg_exec ($db, $select);
But giving me the following error:
Warning: pg_exec(): Query failed: ERROR: column "createdon" does not
exist in /data/httpd/VirtualHosts/web-sandbox/htdocs/_testing/union.php
on line 34
The column 'createdon' does exist in both so I can only assume that my
ORDER BY command isnt properly formated. Is this the correct way to
ORDER a UNION SELECT?

Cheers

Burnsy

Nov 10 '05 #7
"bizt" <bi******@yahoo.co.uk> wrote:
Ok, tried a simple example:

$select = "SELECT title, desctext FROM commnews UNION SELECT title,
article AS desctext FROM pressnews ORDER BY createdon";

$result = pg_exec ($db, $select);

But giving me the following error:

Warning: pg_exec(): Query failed: ERROR: column "createdon" does not
exist in /data/httpd/VirtualHosts/web-sandbox/htdocs/_testing/union.php
on line 34

The column 'createdon' does exist in both so I can only assume that my
ORDER BY command isnt properly formated. Is this the correct way to
ORDER a UNION SELECT?


Almost. The union-ed query does not have column names, so you have to
refer to them by number. That means the column must participate in the
query. This should work:

SELECT title, desctext, createdon FROM commnews
UNION
SELECT title, article, createdon FROM pressnews
ORDER BY 3;
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 11 '05 #8

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

Similar topics

8
by: VisionSet | last post by:
I have already posted this under 'Simple table organisation question' here is a more lucid version. It will be under MySQL v4.0 which now supports unions Consider 2 entities - books & authors...
8
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
57
by: Bing Wu | last post by:
Hi all, I am running a database containing large datasets: frames: 20 thousand rows, coordinates: 170 million row. The database has been implemented with: IBM DB2 v8.1
3
by: Michael Thomas | last post by:
Hi everyone Is there any way to compare tables and automatically update values. I import a list of products with associated cost prices and selling prices from an external database into a table...
11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
10
by: Jim Devenish | last post by:
I have a split front end/back end system. However I create a number of local tables to carry out certain operations. There is a tendency for the front end to bloat so I have set 'compact on...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
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...
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:
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
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...

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.