5

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

For example:

userid = 1

I want to select data from all 3 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 it doesn't seem to work.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Latox
  • 4,655
  • 15
  • 48
  • 74
  • 2
    What does "doesn't seem to work" mean? – OMG Ponies Feb 20 '11 at 07:24
  • 2
    Insufficient information for meaningful answer. Please post the schema of all the tables, more info about what you're trying to do, what the expected results are, what the actual results are, and any error messages. If using PHP, please enable all errors and warnings. – MarkR Feb 20 '11 at 07:56

3 Answers3

4

with 3 fields all the same

So you mean that you want the same 3 fields from all 3 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

The fields don't have to be named the same, but the same types need to line up in position 1, 2 and 3.

The way the limits work is:

  • it will attempt to get 30 from random.
  • If it has 30 already, it won't even look at the other 2 tables
  • if it has less than 30 from random, it will try to fill up to 30 from pandom and only finally landom
Ronnis
  • 12,593
  • 2
  • 32
  • 52
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
2
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'
Weltkind
  • 687
  • 5
  • 9
1
SELECT * FROM `random`
         JOIN `pandom` USING (`userid`)
         JOIN `landom` USING (`userid`)
WHERE `userid`='1'
awm
  • 6,526
  • 25
  • 24