Retrieving all values โ€‹โ€‹from three different tables. JOIN task?

I am learning mysql and trying to get this to work, but the error always comes up no matter how I try it, or it just shows nothing from the table.

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

I want to list all values โ€‹โ€‹in alphabetical order by name

Tables: General - Temp - Location

Columns: id - url - title - description

How would you write a select statement?

I've tried many ways and can't figure it out ... I guess it will look like

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

      

I have been actively playing around with changing values โ€‹โ€‹like Temp.title = General.title and doesn't seem to help

Any ideas?

Thank!

ps - I also tried this and it gives data but only shows a few results from the total table with the number of messages 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

      

+2


source to share


2 answers


(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 Link .

+4


source


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 that you want all columns from the additional table. If you don't, do not include the extra column, but make sure the data types match the position in the SELECT statement.

+1


source







All Articles