tags:

views:

49

answers:

2

I'm learning mysql and have been trying to get this working, but an error always comes up no matter how I try it - or it just doesn't show anything from the table.

I have 3 tables, all with the same amount of columns and name of columns

I want to list all the values in alphabetical order by title

Tables: General - Temp - Location

Columns: id - url - title - description

How would you write the select statement?

I have tried numerous ways and can't figure it out... I'm guessing it would look something like

SELECT * FROM General JOIN Temp ON General.title = Temp.title JOIN Location ON Temp.title = General.title

I have played around extensively with changing the values like Temp.title = General.title and that doesnt seem to help

Any ideas?

Thanks!

p.s - I also tried this and it gives data, but only shows a few results from the general table with the amount of posts from temp.. very confusing

SELECT
   General.id,
   General.url,
   General.title,
   General.description,
   Temp.id,
   Temp.url,
   Temp.title,
   Temp.description,
   Location.id,
   Location.url,
   Location.title,
   Location.description
FROM
   General INNER JOIN Temp
   ON General.id = Temp.id
   INNER JOIN Location
   ON Temp.id = Location.id
ORDER BY
   General.title
+4  A: 
(SELECT id, url, title, description FROM General)
UNION 
(SELECT  id, url, title, description FROM Temp)
UNION 
(SELECT id, url, title, description FROM Location)
ORDER BY Title

MySQL Reference.

Mitch Wheat
thank you! is there a way I could use this syntax while adding a table that has an extra column?
krio
so helpful thank you!!!
krio
@krio: sure. just include a 'dummy' value in that column position for the other queries that do not have that column
Mitch Wheat
+1  A: 

To add a table with an extra column:

SELECT NULL 'id',
       NULL 'url',
       NULL 'title',
       NULL 'description',
       NULL 'extra_column'
  FROM DUAL
UNION
SELECT g.id, 
       g.url, 
       g.title, 
       g.description,
       NULL
  FROM GENERAL g
UNION 
SELECT t.id, 
       t.url, 
       t.title, 
       t.description,
       NULL
  FROM TEMP t
UNION 
SELECT l.id, 
       l.url, 
       l.title, 
       l.description,
       NULL
  FROM LOCATION l
ORDER BY title

This assumes you want all the columns from the extra table. If you don't, don't include the extra column but make sure the data types match for the position in the SELECT statement.

OMG Ponies
thank you! this answer also helped me get a better understanding of how to select in sql
krio