mysql数据库关联查询【lert join】常见使用

一,关键词:

  1) left(连接类型)   join  on(条件)

二,常见连接:

  1)join(获取的是一个笛卡尔积)、

    select * from t_table1  join t_table2 ;

  2)左连接 (两表关联,在匹配条件之后,左表保留全部,也就是包含没有匹配到的字段)

    select * from t_table1  left join t_table2 on t_table1.id = t_table2 .id;

  3)右连接(两表关联,在匹配条件之后,右表保留全部,包含没有匹配到的字段)

    select * from t_table1  right join t_table2 on t_table1.id = t_table2 .id;

  4)内连接 (获取交集 )

    select * from t_table1  inner join t_table2  on t_table1.id = t_table1  .id;

  5)只查左表内容(两表关联,查询左表独有的数据)

    select * from t_table1  left join t_table2  on t_table1.id = t_table2  .id where t_table2.id is null;

  6)只查右表内容(两表关联,查询右表独有的数据)

    select * from t_table1  left join t_table2  on t_table1.id = t_table2  .id where t_table1.id is null;

  7)全连接

    select * from t_table1  left join t_table2  on t_table1 .id = t_table2.id

      union

    select * from t_table1  right join t_table2  on t_table1 .id = t_table2.id;

  8)去交集

    select * from t_table1  left join t_table2  on t_table1.id = t_table2  .id where t_table2  .id is null

       union

    select * from t_table1  right join t_table2  on t_table1.id = t_table2  .id where t_table1  .id is null;

  

时间: 2024-07-30 17:14:15

mysql数据库关联查询【lert join】常见使用的相关文章

Yii2实现跨mysql数据库关联查询排序功能

遇到一个项目,需要跨表网上找了很多的资料,整理一下,方便以后再次使用 背景:在一个mysql服务器上(注意:两个数据库必须在同一个mysql服务器上)有两个数据库: memory (存储常规数据表) 中有一个 user 表(记录用户信息) memory_stat (存储统计数据表) 中有一个 user_stat (记录用户统计数据) 现在在 user 表生成的 GridView 列表中展示 user_stat 中的统计数据 只需要在User的model类中添加关联. public functio

Yii2中多表关联查询(join、joinwith) with是不执行sql的

Yii2中多表关联查询(join.joinwith) 我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name customer_id book_id) 图书表 (id book_name author_id) 作者表 (id author_name) 模型定义 下面是这4个个模型的定义,只写出其中的关联 Customer class Customer extends \

Yii2中多表关联查询(join、joinwith)

我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_name       customer_id   book_id) 图书表                    (id  book_name       author_id) 作者表                    (id  author_name) 模型定义 下面是这4个个模型的定义,只写

MySQL 查询优化 - 关联查询

1. 关联查询执行流程 MySQL执行关联查询的策略很简单,他会从一个表中循环取出单条数据,然后用该条数据到下一个表中寻找匹配的行,然后回溯到上一个表,到所有的数据匹配完成为止.因此也被称为"嵌套循环关联". 来看下面这个SQL: select tb1.col1, tb2,col2 from tb1 inner join tb2 using(col3) where tb1.col1 in (5,6) 他的执行顺序为(伪代码): List outerDataList = "se

MySQL数据库的查询缓冲机制

MySQL数据库的查询缓冲机制 2011-08-10 11:07 佚名 火魔网 字号:T | T 使用查询缓冲机制,可以极大地提高MySQL数据库查询的效率,节省查询所用的时间.那么查询缓冲机制是怎样设置的呢?本文我们就来介绍这部分内容,希望能够对您有所帮助. AD: MySQL数据库提供了查询缓冲机制.使用该查询缓冲机 制,MySQL将SELECT语句和查询结果存放在缓冲区中,以后对于同样的SELECT语句(区分大小写),将直接从缓冲区中读取结果.以节省查询时 间,提高了SQL查询的效率.本文

MySQL数据库like查询中文出现不准确的解决方法

2013-02-18 1,502阅 评论( 暂无评论 ) 更多0 Mysql数据库like查询中文出现不准确的解决方法,中文检索有时候有点蛋疼,Mysql数据库like查询中文有时候会出现不准确.而且,在进行like检索时,有时候会返回一些与查询词不相关的记录,如查找 “%s%” 时,返回的结果中可能有中文字符,却没有s字符存在,这与数据库中文编码规则有关. 如希望查找title中含有字母s的所有新闻: select * from test.news where title like '%s%'

Mysql数据库大小查询

1.进入 information_schema 数据库(存放了其他的数据库的信息) use information_schema; 2.查询所有数据的大小: SELECT concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data FROM TABLES; 3.查看指定数据库的大小: 比如查看数据库hellodb的大小 SELECT concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as dat

php 基本连接mysql数据库和查询数据

连接数据库,有三种方法 1. 常规方式: $con=mysql_connect($dbhostip,$username,$userpassword) or die("Unable to connect to the MySQL!"); $db = mysql_select_db($dbdatabasename,$con);//执行语句 $qres=mysql_query("SELECT id,GoodsName FROM user"); //提取一条数据 11 $r

MySQL数据库联合查询与连接查询

联合查询 基本概念 联合查询是可合并多个相似的选择查询的结果集.等同于将一个表追加到另一个表,从而实现将两个表的查询组合在一起,使用为此为UNINO或UNION ALL 联合查询:将多个查询的结果合并到一起(纵向合并):字段数不变,多个查询的记录数合并 应用场景 1.将同一张表中不同的结果(需要对应多条查询语句来实现),合并到一起展示数据 2.最常见:在数据量大的情况下,会对表进行分表操作,需要对每张表进行部分数据统计,使用联合查询来将数据存放到一起显示 基本语法 select 语句 union