从多个表中选择数据? [英] SELECT Data from multiple tables?

查看:73
本文介绍了从多个表中选择数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表,所有3个字段都相同.我基本上想从每个表中选择信息

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

例如:

userid = 1

我想从所有3个表中选择数据,其中userid = 1

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

我当前正在使用:

   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

但这似乎不起作用.

推荐答案

具有3个相同的字段

with 3 fields all the same

所以您是说要从所有3个表中都the same 3 fields吗?

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

字段的名称不必相同,但相同的类型需要在位置1、2和3处对齐.

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

限制的工作方式是:

  • 它将尝试从random获取30.
  • 如果已经有30张,它甚至不会查看其他2张桌子
  • 如果random中的数量少于30,它将尝试填充pandom中的30,直到最后landom
  • 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

这篇关于从多个表中选择数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆