Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23602
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 54
  • 用 户 组: 普通用户
  • 注册时间: 2021-04-25 14:19
个人简介

专注于云计算

文章分类
文章存档

2021年(12)

我的朋友

分类: Mysql/postgreSQL

2021-04-29 09:33:53

原文地址:MySQL 内链接 作者:starb6


You need not have two different tables to perform a join. Sometimes it is useful to join a table to itself, if you want to compare records in a table to other records in that same table. For example, to find breeding pairs among your pets, you can join the pettable with itself to produce candidate pairs of live males and females of like species:

mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species FROM pet AS p1 INNER JOIN pet AS p2 ON p1.species = p2.species AND p1.sex = 'f' AND p1.death IS NULL AND p2.sex = 'm' AND p2.death IS NULL;





mysql>
SELECT pet.name, TIMESTAMPDIFF(YEAR,birth,date) AS age,
                        remark
FROM pet
                         
INNER JOIN event
                          
ON pet.name = event.name
                          
WHERE event.type = 'litter';





 
阅读(1417) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~