2

im trying to select data from multiple tables, now i am fine with doing it with two tables as i do a query like so:

$myquery = sql_query(
    "SELECT a.object_title, a.published_by, b.userid
     FROM table1 AS a 
     JOIN table2 AS b ON (a.published_by = b.userid)"
);

But see now, i want to select data from a third table, however this third table does not have relationship such as primary key between the first two tables, so i simply want to just pull data from it and form any sort of link with a "JOIN".

How would simply add the third to this query?

Thanks

marcosh
  • 8,780
  • 5
  • 44
  • 74
user3332590
  • 101
  • 1
  • 1
  • 11
  • 1
    Write a seperate query? Since it's not related, you don't need to write it in the above query. – davey Apr 07 '14 at 08:32
  • I cant do this, as it will overwrite my first query as i need them both in the same section. – user3332590 Apr 07 '14 at 08:34
  • You could simply do this: `SELECT a.object_title, a.published_by, b.userid, c.id, c.field FROM table1 AS a, table3 as c` Just keep in mind that this will preform a Cartesian product with this table. – Fadey Apr 07 '14 at 08:37
  • can you tell me what you mean by cartesian product? – user3332590 Apr 07 '14 at 08:42
  • You should also have a look at this [Q&A](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) that I wrote which covers a lot of this sort of thing in detail. – Fluffeh Apr 23 '14 at 11:46

3 Answers3

2

You could use CROSS JOIN :

$myquery = sql_query(
    "SELECT a.object_title, a.published_by, b.userid, c.whatever
     FROM table1 AS a 
     JOIN table2 AS b ON (a.published_by = b.userid)
     CROSS JOIN table3 AS c"
);

I used this other post to find the idea.

More infos here.

Community
  • 1
  • 1
Theox
  • 1,363
  • 9
  • 20
0

Add within you query a Left Join.

0
$myquery = sql_query(
            "SELECT a.object_title, a.published_by, b.userid, c.column_name
             FROM Table1 AS a 
             JOIN Table2 AS b ON (a.published_by = b.userid)
             CROSS JOIN Table3 AS c"
);
halfer
  • 19,824
  • 17
  • 99
  • 186
Mayuri
  • 402
  • 6
  • 13