WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-19-2007, 01:17 AM
MichelleStillCantType MichelleStillCantType is offline
WebProWorld Member
 

Join Date: Jan 2005
Location: Ohio
Posts: 30
MichelleStillCantType RepRank 0
Default How to Query 2 Tables (Not a join?)

Uuuuughhhh! Frustration. This seems like it should be so simple and I'm lost.

I want to randomly rotate (with each load or refresh) a section of text, corresponding images and links, etc. with a limit of 1 on a specific webpage. (The text I show on this page is also limited by a "WHERE" statement).

All is great if I only query from one table. Simple enough...

SELECT * FROM tableA WHERE level= '1' ORDER BY reverse(rand()) LIMIT 1

I randomly get my info from tableA with each refresh just fine with the above code.

BUT...

I also need to select from tableB, tableC, tableD and so on. All fields are the same in tableA, tableB and so on. I've tried...

SELECT * FROM tableA,tableB,tableC WHERE level= '1' ORDER BY reverse(rand()) LIMIT 1

...amongst other things. I'm just not getting it. Is something that seems so simple impossible?

Only one section of text can show at a time and this will be from up to 50 tables if I get it worked out.

I do SOME programming. Minimal. Enough to do what I need here and there (like not having to update 100's of html pages for one little alphabetical addition) but you wouldn't call me a programmer. Help? In simple terms..OR even exact code
Reply With Quote
  #2 (permalink)  
Old 10-19-2007, 07:41 AM
LobsterWebDesign LobsterWebDesign is offline
WebProWorld New Member
 

Join Date: Oct 2007
Location: Cardiff, South Wales
Posts: 21
LobsterWebDesign RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

I would write a script to choose the table first by random and so put your table names in a seperate table and then use your sql to pick the table name. Hold this in a variable and use it in your current script.
Reply With Quote
  #3 (permalink)  
Old 10-19-2007, 09:28 AM
Dubbya's Avatar
Dubbya Dubbya is offline
WebProWorld 1,000+ Club
 

Join Date: Nov 2006
Location: Steinbach, Manitoba, Canada
Posts: 1,168
Dubbya RepRank 3Dubbya RepRank 3
Default Re: How to Query 2 Tables (Not a join?)

Tech republic has a concise little article on how to do this with or without Joins or unions. You should be able to find a select statement you can use.

Check it out:
Code:
SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;
SQL basics: Query multiple tables
__________________
Printer ink, inkjet & toner cartridges in Canada
"Price-wise printing supplies"
inkjetOasis.ca
Reply With Quote
  #4 (permalink)  
Old 10-19-2007, 04:17 PM
rgcote rgcote is offline
WebProWorld New Member
 

Join Date: Sep 2005
Posts: 6
rgcote RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

Hmmm. Why do you need all those tables? If the structure is the same between all the tables, then maybe you'll have an easier time by combining all the data into a single table and add an additional field that marks each record as "A", "B", "C", etc.

I am assuming that you put them in different tables because you need to track the records in each table separate from the records in the other tables. If that is the case, then combining them into a single table and tagging them appopriately might be a better way to go.
Reply With Quote
  #5 (permalink)  
Old 10-19-2007, 04:31 PM
imvain2 imvain2 is offline
WebProWorld Pro
 

Join Date: Apr 2004
Posts: 266
imvain2 RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

Dubbya has the correct query structure, however rgcote has a great point. I do exactly what he is talking about and it works great.
Reply With Quote
  #6 (permalink)  
Old 10-19-2007, 04:37 PM
kgun's Avatar
kgun kgun is offline
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 4,696
kgun RepRank 3kgun RepRank 3
Default Re: How to Query 2 Tables (Not a join?)

Not a join ? (Note the question mark).

I take the idea of creating a temporary table, but joins are an advanced SQL topic.

Why make it complicated when you can make it simple?

Last edited by kgun : 10-19-2007 at 04:42 PM.
Reply With Quote
  #7 (permalink)  
Old 10-19-2007, 05:06 PM
MichelleStillCantType MichelleStillCantType is offline
WebProWorld Member
 

Join Date: Jan 2005
Location: Ohio
Posts: 30
MichelleStillCantType RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

Thank you all.

Dubbya, I have been trying that and just can't figure out what I'm doing wrong because it wont work for me. I have pulled out half my hair...lol.

The reason I have so many tables with all the same fields is that eventually (future expansion) after some growth, thesee tables will have fields added that are NOT the same.

On another site I did it with just the one table and called up needed info by a category field - but - then when the many different fields were added for the many different categories it ended up becoming a pain in the you know for many reasons. I'd rather have the different tables if I can get it to work.
Reply With Quote
  #8 (permalink)  
Old 10-19-2007, 05:18 PM
Syspira Syspira is offline
WebProWorld New Member
 

Join Date: Nov 2003
Location: Toronto
Posts: 15
Syspira RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

If you're trying to do a multi-table select by using the simple syntax (WHERE a.col1 = b.col1 etc.) and it's not working, check to see if you have any null values in any of the columns you're trying to select. That can sometimes cause that sort of query to fail.

If that is the case, you need to use a Join instead.

This has tripped me up on more than one occasion, but Join (usually a Left Join, with the most important table listed to the left, i.e. first) generally sorts it out.
Reply With Quote
  #9 (permalink)  
Old 10-19-2007, 06:56 PM
kgun's Avatar
kgun kgun is offline
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 4,696
kgun RepRank 3kgun RepRank 3
Default Re: How to Query 2 Tables (Not a join?)

Note, you are working on a relational database. Then any complete overlapping of fields (columns) in the tables is wasted work.

Last edited by kgun : 10-19-2007 at 07:00 PM.
Reply With Quote
  #10 (permalink)  
Old 10-20-2007, 06:28 AM
jammybiskit jammybiskit is offline
WebProWorld Member
 

Join Date: Feb 2006
Location: UK
Posts: 25
jammybiskit RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

One way to do this is to use the union statement which is standard in most modern SQLs. The order by clause comes after all the unions. For two tables tablea and tableb this looks like:

select * from tablea where level=1 union select * from tableb where level=1 order by reverse(rand()) limit 1;

For three tables a,b,c:
select * from tablea where level=1 union select * from tableb union select * from tablec where level=1 order by reverse(rand()) limit 1;

and so on

The tables must all have the same columns for this to work.

This works on MySQL but is probably not efficient - it may hit your server hard if you have as many as 50 tables. You don't say how large these tables are or are expected to become.

You should consider amalgamating the tables into one with an extra column to distinguish between a,b,c and so on as rgcote suggested. You may need to do this if the performance is poor.
__________________
There may be no such thing as a silly question but the world is littered with silly answers.
Reply With Quote
  #11 (permalink)  
Old 10-20-2007, 09:39 AM
kgun's Avatar
kgun kgun is offline
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 4,696
kgun RepRank 3kgun RepRank 3
Default Re: How to Query 2 Tables (Not a join?)

Quote:
Originally Posted by jammybiskit View Post
You should consider amalgamating the tables into one with an extra column to distinguish between a,b,c and so on as rgcote suggested. You may need to do this if the performance is poor.
Yes that is a possible solution, like pulling fields / records from the table with PHP or another scripting language.
  1. What is the minimalistic solution that should be learned sooner or later?
  2. If (s)he can not use some type of joins, I question the structure of the database (collection of tables).
Why not ask on the MySQL forum?

Last edited by kgun : 10-20-2007 at 09:41 AM.
Reply With Quote
  #12 (permalink)  
Old 10-20-2007, 12:49 PM
lyricsenergy lyricsenergy is offline
WebProWorld New Member
 

Join Date: Oct 2007
Posts: 11
lyricsenergy RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

1. temporary table2. temporary array
3. ?
Reply With Quote
  #13 (permalink)  
Old 10-21-2007, 09:21 PM
mccreath's Avatar
mccreath mccreath is offline
WebProWorld New Member
 

Join Date: May 2004
Location: Australia
Posts: 18
mccreath RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

"The reason I have so many tables with all the same fields is that eventually (future expansion) after some growth, these tables will have fields added that are NOT the same."

A structure that might work for you is to move all the common fields onto a base table and only have the custom fields in the separate tables (a,b,c). You can then do the single query for your random search while still allowing you to customise each type. e.g.

#searching just a
select * from a, base where a.baseid = base.id ...

# searching all
select * from base ...
__________________
Tony McCreath
Motown - Nexus
http://www.grunt.tv/
Reply With Quote
  #14 (permalink)  
Old 10-22-2007, 04:45 AM
jammybiskit jammybiskit is offline
WebProWorld Member
 

Join Date: Feb 2006
Location: UK
Posts: 25
jammybiskit RepRank 0
Default Re: How to Query 2 Tables (Not a join?)

"The reason I have so many tables with all the same fields is that eventually (future expansion) after some growth, thesee tables will have fields added that are NOT the same."

Using * (the catch all columns identifier) will not work with union if the tables have different columns.

Assuming that each of your 50 tables has a column called 'level' and another called 'result', say, (maybe with some other columns as well), you can use the statement

select result from tablea where level=1 union select result from tableb union select result from tablec where level=1 ... order by reverse(rand()) limit 1;

This is going to get large and difficult to maintain for 50 tables.

Use a single table instead of having 50 - which would mean a much simpler select. You can add columns to a table in MySQL and others using 'alter table'.

If you had a single table then your statement would be

select result from mytable where level=1 order by reverse(rand()) limit 1;

Adding columns would not affect this statement.
__________________
There may be no such thing as a silly question but the world is littered with silly answers.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: join, query, tables



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL query works on CLI or Query Browser but NOT in PHP CraigB Database Discussion Forum 4 09-28-2006 10:39 AM
Why Did I Join This Group? DoctorMason Database Discussion Forum 2 05-09-2006 07:47 PM
To join or not to join? oldmaid Breakroom (General: Any Topic) 5 08-30-2005 01:37 PM
JOIN.EXE WPW_Feedbot IT Discussion Forum 0 07-25-2005 01:02 PM


Search Engine Friendly URLs by vBSEO 3.0.0