SELECT Data from multiple tables?

I have 3 tables, all three fields. I basically want to select information from each table

For instance:

userid = 1

I want to select data from all three tables, where userid = 1

I am currently using:

   SELECT r.*, 
          p.*, 
          l.*
     FROM random r
LEFT JOIN pandom p ON r.userid = p.userid
LEFT JOIN landom l ON l.userid = r.userid
    WHERE r.userid = '1'
    LIMIT 0, 30

But that does not work.

+5
source share
3 answers

with three fields anyway

So you want you the same 3 fieldsout of all three tables?

   SELECT r.col1, r.col2, r.col3
     FROM random r
    WHERE r.userid = '1'
    LIMIT 0, 30
UNION ALL 
   SELECT p.pcol1, p.pcol_2, p.p3
     FROM pandom p
    WHERE p.userid = '1'
    LIMIT 0, 30
UNION ALL 
   SELECT l.l1, l.l2, l.l3
     FROM landom l
    WHERE l.userid = '1'
    LIMIT 0, 30

Fields should not be called identical, but the same types should line up at positions 1, 2, and 3.

How restrictions work:

  • he will try to get 30 out random.
  • If he already has 30, he will not even consider the other 2 tables
  • if he has less than 30 from random, he will try to fill up to 30 from pandomand only finallylandom
+4
SELECT t1.*, t2.*, t3.* 
   FROM `random` as t1, `pandom` as t2, `landom` as t3 
WHERE t1.`userid`='1' AND t2.`userid`='1' AND t3.`userid`='1'
+2
SELECT * FROM `random`
         JOIN `pandom` USING (`userid`)
         JOIN `landom` USING (`userid`)
WHERE `userid`='1'
+1
source

All Articles