Forum Moderators: open

Message Too Old, No Replies

Select 1 row from 1 table and ALL rows from a second table

is this possible?

         

neophyte

8:16 am on Oct 26, 2007 (gmt 0)

10+ Year Member



Hello All -

I've got a rather special circumstance in which I would like to to select one row from one table and then all rows from another table. I'm able to do this with two select statements but I'm wondering if it's possible to do this all at one time?

I've already tried different types of joins, but so far, without success.

The pseudo statement would be:

SELECT ALL FROM table_content where page_id = '1' AND SELECT ALL (EVERY ROW) FROM table_sitemap.

I imagine this can be done, I just don't know the correct syntax.

Can someone give me an idea of how (and if) this can be done? Or am I stuck with two select statements?

Neophyte

Discovery

8:48 am on Oct 26, 2007 (gmt 0)

10+ Year Member



I am not getting exactly, what your your requirement ,

do you want something like this,

SELECT *
FROM table_sitemap
WHERE page_id
IN (SELECT page_id FROM table_content page_id = '1');

neophyte

9:39 am on Oct 26, 2007 (gmt 0)

10+ Year Member



discovery -

Thanks for your reply.

A little background:

table_content contains fields with title tags, keywords and text content. There are many rows in this table - enough rows to hold the textural content of each page in the site. table_content DOES have a primary key.

All that I need from this table is the proper row selected from table_content, which will be the row that has the header text for the sitemap page (page_id = '1').

table_sitemap contains a bunch of rows; each row contains "section" and "page" fields for building a sitemap for this particular project.

I need ALL of the rows from table_sitemap.

So, this is the problem: I'm trying to join only one row of data from one table (table_content) and all the rows of data from another table with just one SELECT statement... which I'm not sure can be done.

So, another way to put the pseudo code would be:

SELECT *
FROM table_sitemap
AND
SELECT *
FROM table_content
WHERE table_content.page_id = '1';

That's what I'm trying to do; is that clearer?

Neophyte

bmcgee

6:02 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



If those tables have the same fields in them, then just UNION the selects.

However, I highly doubt they are the same fields. In which case, why the heck are you trying to pull them out in one query? It makes no sense to do what you are trying to do.

If you wanted to be ridiculously inefficient you could join the tables and bring back the duplicate all the single-row table into all the records from the other table.

What's the problem with 2 selects?

LifeinAsia

6:17 pm on Oct 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If you're always pulling all the rows from the second table, then there is no real correlation between that table and the first.

Just do 2 selects.

neophyte

12:29 am on Oct 27, 2007 (gmt 0)

10+ Year Member



Thanks for everyone's input.

This was just more experimental than anything else ... just wanted to see if it could be done with one select, but, 2 selects it will remain.

Thanks again!

syber

7:47 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



I agree that 2 selects would be the most efficent, however, if you really want to do this you could use a Cartesian Join.

SELECT *
FROM table_sitemap, table_content
WHERE table_content.page_id = '1'

neophyte

12:29 am on Oct 28, 2007 (gmt 0)

10+ Year Member



Syber -

Tried your statement and it worked great! Thanks!

As is probably obvious, I'm still learning (and many times stumbling with) sql statements...which is why I asked the question.

It appears that from the standpoint of efficiency (which in this case I mean query/data-display speed?) that the fewer selects you have to grab data for a particular page, the better. Is this assumption correct?

Neophyte

syber

8:15 pm on Oct 28, 2007 (gmt 0)

10+ Year Member



It appears that from the standpoint of efficiency (which in this case I mean query/data-display speed?) that the fewer selects you have to grab data for a particular page, the better. Is this assumption correct?

Generally, that would be a correct assumption (that is why subqueries are so powerful). The danger with a Cartesian Join is that it can create a tremendous amount of duplicate data. In this case, since only one row is selected from the one table, the impact is minimal.