zoukankan      html  css  js  c++  java
  • mysql合并表

    有如下两张表

    a
    +------+------+---------+
    | uid  | name | addtime |
    +------+------+---------+
    |    1 | tom  |    2001 |
    |    2 | jack |    2002 |
    |    3 | alex |    2003 |
    +------+------+---------+
    
    b
    +------+-------+---------+
    | uid  | title | addtime |
    +------+-------+---------+
    |    1 | rain  |    2004 |
    |    2 | snow  |    2005 |
    |    3 | wind  |    2006 |
    +------+-------+---------+

    join是将两个表横向合并,就是通过on的条件将一个表追加在另一个表后边,新表相对于原来的两个表字段增多了。

    mysql> select * from b left join a on b.uid = a.uid;
    +------+-------+---------+------+------+---------+
    | uid | title | addtime | uid | name | addtime |
    +------+-------+---------+------+------+---------+
    | 1 | rain | 2004 | 1 | tom | 2001 |
    | 2 | snow | 2005 | 2 | jack | 2002 |
    | 3 | wind | 2006 | 3 | alex | 2003 |
    +------+-------+---------+------+------+---------+ 

    union是将两个表竖向合并,新表字段不会多于原表。

    mysql> select * from b union select * from a;
    +------+-------+---------+
    | uid  | title | addtime |
    +------+-------+---------+
    |    1 | rain  |    2004 |
    |    2 | snow  |    2005 |
    |    3 | wind  |    2006 |
    |    1 | tom   |    2001 |
    |    2 | jack  |    2002 |
    |    3 | alex  |    2003 |
    +------+-------+---------+
    
    mysql> select title,addtime from b union select name,addtime from a; 
    +-------+---------+                                                  
    | title | addtime |                                                  
    +-------+---------+                                                  
    | rain  |    2004 |                                                  
    | snow  |    2005 |                                                  
    | wind  |    2006 |                                                  
    | tom   |    2001 |                                                  
    | jack  |    2002 |                                                  
    | alex  |    2003 |                                                  
    +-------+---------+                                                  

    参考:http://bbs.csdn.net/topics/390410010

  • 相关阅读:
    MVC3分页传2参
    C# 二进制存储图片到mssql(一)
    著名黑客组织[转]
    浅看C# md5加密
    google搜索技巧
    字符串编码转换 GBK utf8
    objectivec 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
    NSPredicate的用法
    Java关键字final、static使用总结()
    CGAffineTransform相关函数
  • 原文地址:https://www.cnblogs.com/hujq1029/p/7649803.html
Copyright © 2011-2022 走看看