mysql 二个表on_MySQL如何在两个字段上连接表(MySQL how to join tables on two fields)

MySQL如何在两个字段上连接表(MySQL how to join tables on two fields)

我有两个date和id字段的表。 我想加入这两个领域。 我试过了

JOIN t2 ON CONCAT(t1.id, t1.date)=CONCAT(t2.id, t2.date)

这是有效的,但它很慢。 有没有更好的方法来做到这一点?

I have two tables with date and id fields. I want to join on both fields. I tried

JOIN t2 ON CONCAT(t1.id, t1.date)=CONCAT(t2.id, t2.date)

that works, but it is very slow. is there a better way to do this?

原文:https://stackoverflow.com/questions/498197

更新时间:2019-06-24 20:38

最满意答案

JOIN t2 ON t1.id=t2.id AND t1.date=t2.date

JOIN t2 ON t1.id=t2.id AND t1.date=t2.date

2009-01-31

相关问答

JOIN t2 ON t1.id=t2.id AND t1.date=t2.date

JOIN t2 ON t1.id=t2.id AND t1.date=t2.date

在那里 select a.*

from pos_tables a left join

pos_user_login b1 on a.staff1=b1.staff_id left join

pos_user_login b2 on a.staff2=b2.staff_id left join

pos_user_login b3 on a.staff3=b3.staff_id left join

pos_user_login b

...

正如Lan已经说过的那样,你无法在Database Expert中执行此操作,并且需要手动通过SQL命令添加SQL作为报表的数据源。 您已经拥有要使用的查询,因此应该很容易: 在创建报告时,不要选择要添加的不同表,而只需选择连接下最顶层的选项“添加命令”,这样您就可以将该查询粘贴到其中并进行使用。 您不必担心可视链接字段等,并且您将能够执行数据库支持的所有类型的连接,但Crystal不能。 I have found a solution to this without using Sql Comm

...

count计算给定参数的非null值的数量。 您拥有的dis_id每个注释创建一行,其中dis_id和com_id都不为null ,因此它们的计数将相同。 由于这些是ID,您可以只计算distinct的出现次数以获得您想要的响应: (编辑:根据评论中的请求添加了一个order by子句) SELECT p.PRO_Name,

COUNT(DISTINCT d.DIS_Id) AS nofdisc,

COUNT(DISTINCT c.COM_Id) A

...

使用 select ifnull(c.area, s.area) as area,

sum( IF(b.bill_type = 'sales', bd.quantity*(bd.price-bd.discount), 0) ) as totalSales,

sum( IF(b.bill_type = 'salesReturns', bd.quantity*(bd.price-bd.discount), 0) ) as totalReturns,

su

...

您需要从Country到User实体的@ManyToOne映射以及从User到Country相应@ManyToOne映射: @Entity

@Table(name = "country")

public class Country {

@Id

@Column (name = "code")

private String code;

@Column (name = "name")

private String name;

@OneToMany(m

...

将区域表加入单位表两次; 每个地区类型一次。 使用别名来区分分区表的每个实例。 如果您愿意,可以包含可选的AS关键字。 Join the districts table to the units table twice; once for each district type. Use aliases to distinguish each instance of the district table. If you like, you can include the optional AS ke

...

首先为两者添加共同的id,并得到像这样我只把这个代码例如 events => id ,evnt_id,evnt_nm_,日期,地点,详情 参与者=> p_id, common_id ,p_nm,组织,联系人 SELECT events.id,events.evnt_id, events.evnt_nm_, events.date, events.venue, events.details FROM events INNER JOIN participants ON articipants.comm

...

需要一个简单的Join,参见更多: MySQL加入手册 SELECT

table1.id,table1.name,

table2.name2,table2.upvote,

table2.downvote

FROM `table1`

INNER JOIN `table2`

ON

table2.name2 = table1.id

A simple Join is needed, See more : MySQL Joins Manual SELECT

table1.id,table1.name

...

你的联接应该没问题。 第三种方式: select * from Table_1 a

left join Table_2 b on a.type = b.type

如果您没有得到任何结果,那么type列的值不相等 。 更新 鉴于您的注释声明Table_1.type是Table_1.type的子字符串,您可以更改连接运算符: select * from Table_1 a

left join Table_2 b on b.type LIKE '%' + a.type + '%'

这种做法并不理想。

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值