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
Senior Member
 
freelancr's Avatar
 
Join Date: Oct 2006
Posts: 2,060
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: 2,008
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: 694
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,596
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
Old 21-05-2008, 22:20   #7 (permalink)
sukino
Voice in your head
 
Join Date: May 2008
Posts: 58
Just in case some people still wonders:

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

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

Use a union (since there is no way to link them as there is no relationship between the tables):

Select id, name From Saunas
Where visible = 1
Union
Select id, name From Spas
Where visible = 1

You will get what you need.
  Reply With Quote
Old 12-06-2008, 07:21   #8 (permalink)
arn_e
Registered User
 
Join Date: Jun 2008
Posts: 1
i think this would work

SELECT
*
FROM
Saunas sa , Spas sp
WHERE
sa.visible = 1 AND sp.visible = 1
  Reply With Quote
Old 14-06-2008, 13:19   #9 (permalink)
combatsheep
trained to kill
 
combatsheep's Avatar
 
Join Date: Apr 2008
Location: Liverpool, UK
Posts: 42
Send a message via MSN to combatsheep
It's not really good practice to link tables on fields other that ID's so UNION could be used, it depends on the coders personal choice.

I would personally use UNION
  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