mysql - mysql中同一张表如何比较

标签 mysql

这是一个mysql问题,我什至不知道是否可能,但是正如你在下面看到的,我有2个表。他们基本上做同样的事情,只是有所不同。一个已付款 = 1,另一个已付款 = 0

所以我头痛的是,如果表 2 的 cardidpricetitle 与表 1 中的精确(其中包含paid = 1)一样,那么它不得在表 # 2 中显示。哦,只有当 created 也匹配时才可以进行比较,+ - 5 分钟就可以了。

sql语句1

SELECT t.cardid, ct.title, t.transactionid, FROM_UNIXTIME(t.created),t.priceafterdiscount, t.paid 
FROM transactions as t
left join exp_channel_titles ct on t.restaurant_id = ct.entry_id
where t.paid = 0 
and t.transactiontime > '2013-09-23' and 
t.phoneid != '123456789012345' and 
t.cardid != '88888888' and 
t.restaurant_id NOT in (47505) 
ORDER BY t.created DESC;

我的表 1 的输出。

Card_ID     Title               Trans_ID Created              price   Paid
10017039    Café Cici           15887   2013-09-26 11:04:49  75     0
10017039    Café Cici           15885   2013-09-26 11:03:08  100    0
10017039    Café Cici           15884   2013-09-26 11:02:33  15000  0
10166152    Viet-Nam Nam        15870   2013-09-25 20:51:44  28800  0
10030773    Restaurant Shezan   15866   2013-09-25 20:10:35  38175  0
10030773    Restaurant Shezan   15865   2013-09-25 20:09:41  50900  0
10030773    Restaurant Shezan   15864   2013-09-25 20:08:13  38175  0

sql语句2

SELECT t.cardid, ct.title, t.transactionid, FROM_UNIXTIME(t.created), t.priceafterdiscount, t.paid
FROM transactions as t
left join exp_channel_titles ct on t.restaurant_id = ct.entry_id
where t.paid = 1
and t.transactiontime > '2013-09-23' and 
t.phoneid != '123456789012345' and 
t.cardid != '88888888' and 
t.restaurant_id NOT in (47505) 
ORDER BY t.created DESC

我的表 2 的输出。

Card_ID     Title               Trans_ID Created              price   Paid
10171120    Hjørnet             15889   2013-09-26 11:18:47  6750    1
10017039    Café Cici           15888   2013-09-26 11:06:24  75      1
10017039    Café Cici           15886   2013-09-26 11:04:14  75      1
10129289    Café ZugarBaby      15876   2013-09-25 21:44:34  15000   1
10082903    Café Katz           15862   2013-09-25 19:40:26  19040   1
10064767    Restaurant Fønix    15857   2013-09-25 17:58:53  14250   1

最佳答案

根据您的条件,从表 1 中选择 *,其中行不在表 2 中:

select * from 
table1 
left join table2 
on table1.cardid=table2.cardid 
and table1.price=table2.price 
and table1.title=table2.title and 
timestampdiff(minute,table1.created, table2.created) between -5 and 5

where table2.cardid is null

或者,您可以合并所有两个查询,按 cardid、价格、标题分组,并添加 count(*)=1 和paid=0 的条件,但它不适用于 5 分钟时间戳条件

关于mysql - mysql中同一张表如何比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025038/

相关文章:

.net - 使用 .NET 连接到 MySQL 数据库时出错

php - 从 PHP 导出的 csv 文件中没有显示日期和手机号码

mysql - 无法调整mysql超时

c++ - MySQL C++连接器链接错误

php - MySQL 限制参数相对于行值

SQL:有关 sql 连接的帮助

mysql - mysql错误1442的真正原因是什么?

PHP MySQL 帖子表单

php - MySQL 和 PHP 加载超过 1000 个项目导致浏览器卡住

MySQL ODBC 连接器是 32 位还是 64 位?