Forum Moderators: open
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
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
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?
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
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.