Reply LinkBack Thread Tools Search this Thread
Old 10-04-2008, 06:29   #1 (permalink)
Encantado
Creativo
 
Encantado's Avatar
 
Join Date: Nov 2006
Location: Torrevieja (Alicante), Spain
Posts: 9
SQL problem: Select from multiple tables

Hi all,
I've got a question and hope that somebody can help me with it

The Tables:

Table A : Saunas
- field: id
- field: name
- field: visible

Table B : Spas
- field: id
- field: name
- field: visible

The Question:
How do I select in 1 query every product that has visible = 1.
So I need to display all the Saunas and all the Spas which are visible. These table are not linked with each other.

I have been trying queries like :

SELECT saunas. * , spa_jacuzzis. * FROM saunas, spa_jacuzzis
WHERE saunas.special_offer = '1' AND spa_jacuzzis.special_offer = '1'

But it only shows me the visible saunas

Some help would be great!
  Reply With Quote
Old 10-04-2008, 06:38   #2 (permalink)
freelancr
Web Developer
 
freelancr's Avatar
 
Join Date: Oct 2006
Location: Stratford-upon-Avon, Warwickshire, UK
Posts: 1,848
Send a message via MSN to freelancr Send a message via Skype™ to freelancr
why use 2 tables for that? surely you could just have:

[products]
- id
- name
- type (spa, sauna, etc)
- visible

then just use this query.
Code:
SELECT * FROM `products` WHERE `visible` = '1';

KISS principle - Wikipedia, the free encyclopedia
__________________
  Reply With Quote
Old 10-04-2008, 06:45   #3 (permalink)
Encantado
Creativo
 
Encantado's Avatar
 
Join Date: Nov 2006
Location: Torrevieja (Alicante), Spain
Posts: 9
Because i have in total 9 product tables with each their own specific fields.
  Reply With Quote
Old 10-04-2008, 07:04   #4 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,306
Maybe someone else knows better, but I don't think you can select unrelated data from two different tables in one query.
  Reply With Quote
Old 12-04-2008, 18:22   #5 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 686
Send a message via AIM to d3mcfadden
Quote:
Originally Posted by Encantado
Because i have in total 9 product tables with each their own specific fields.

That is flawed logic and terrible db design. In situations like that you abstract out all fields into their own table and join them back into to one products table.

Anyways, if you want to achieve what you are asking in one query there are two ways to do it, a temporary table or a union command. Using a temp table you select all rows from both tables INTO the temp table them select back out then DROP the temp table. Union will only work if you select the same exact fields from each table.

Code:
SELECT id, name FROM Saunas WHERE visible = 1 UNION SELECT id, name FROM Spas WHERE visible = 1

I would recommend updating your DB schema

Last edited by d3mcfadden : 13-04-2008 at 21:01.
  Reply With Quote
Old 15-04-2008, 01:48   #6 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,128
If you want better control of your data across multiple tables, use a JOIN.

MySQL :: MySQL 5.1 Reference Manual :: 12.2.7.1 JOIN Syntax

That's what it's designed for.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8