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

5
  • 1
    Write a seperate query? Since it's not related, you don't need to write it in the above query. Commented Apr 7, 2014 at 8:32
  • I cant do this, as it will overwrite my first query as i need them both in the same section. Commented Apr 7, 2014 at 8: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. Commented Apr 7, 2014 at 8:37
  • can you tell me what you mean by cartesian product? Commented Apr 7, 2014 at 8:42
  • You should also have a look at this Q&A that I wrote which covers a lot of this sort of thing in detail. Commented Apr 23, 2014 at 11:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Add within you query a Left Join.

Comments

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"
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.