是否可以像这样从两个表中创建涉及行的查询?

时间:2019-04-27 20:49:34

标签: mysql sql

我想创建一个查询,在该查询中,我可以根据可以很好完成的几种条件从表中选择行。但是,我想弄清楚如何仅在该记录的ID在过去24小时内未出现在另一个表中时才显示表中的行。我将如何去做这样的事情。想象一下桌子就像:

ID .          | EMAIL .         | . PASS
------------- | --------------- | ---------------
1 .             abc@abc.com .     rgjoegeijerioge
2 .             ab46c@abc.com .   dgnkjrkjhrnjkrt
3 .             ab46c@abc.com .   etjhrnkrjkjhjhr

------------- | --------------- | --------------- | ---------------
transferID .    ACCOUNT .         ACCOUNTTO .       ITEM
------------- | --------------- | --------------- | ---------------
1 .             abc@abc.com .     rjek@xs.com .     book1
2 .             ab4gtrc@abc.com . xbjk@abc.com      book2
3 .             ab46gggc@abc.com  bca@lo.com        book2

因此,我想从第一个表中获取所有行,在该表中,电子邮件/用户(如果更改表结构,甚至没有userID)都不属于在最后24个表中添加的行小时。

4 个答案:

答案 0 :(得分:3)

您可以使用tidyverse

library(reshape2)
library(tidyverse)

result <- data %>%
  melt(value.name = "col") %>% 
  drop_na() %>%
  mutate(letter   = substr(variable, 3, 3),
         variable = substr(variable, 2, 2)) %>%
  dcast(variable~letter, value.var = "col") %>% 
  rename(Tr = variable)

print(result)
   Tr a b
 1  1 1 2
 2  2 3 4
 3  3 0 5
 4  4 4 2

假设您在NOT EXISTS中有一个名为 SELECT * FROM table1 t1 WHERE NOT EXISTS ( SELECT 0 FROM table2 t2 WHERE t2.account = t1.email AND t2.insertion_date >= DATE_ADD(NOW(), INTERVAL -1 DAY) ) 的列

答案 1 :(得分:1)

替代方法是像Himanshu所述的JOIN:

表格

drop table if exists userinfo;
create table userinfo (
    id int,
    email varchar(200),
    pass varchar(200)
);
insert into userinfo values
(1, 'abc@abc.com', ''),
(2, '123@abc.com', ''),
(3, 'xyz@abc.com', '');

drop table if exists transfers;
create table transfers (
    transferid int,
    account varchar(200),
    accountto varchar(200),
    item varchar(200),
    created_date datetime
);

insert into transfers values
(1, 'abc@abc.com', 't@abc.com', 'Book1', date_sub(now(), interval 1 hour)),
(2, '123@abc.com', 't@abc.com', 'Book2', date_sub(now(), interval 40 hour)),
(3, 'xyz@abc.com', 't@abc.com', 'Book3', date_sub(now(), interval 20 hour));

查询

select u.id, u.email
from userinfo u
left join transfers t
    on u.email = t.account
    and t.created_date >= date_sub(now(), interval 24 hour)
where t.transferid is not null;

结果

id email
1  abc@abc.com
2  xyz@abc.com

示例:https://rextester.com/UYM78253

答案 2 :(得分:0)

您可以使用LEFT JOIN,假设第一个表为table_1,第二个表为table_2

--We only want to check the `id` against `transferid` (simplest situation):
SELECT
    table_1.*
FROM
    table_1 
    LEFT JOIN table_2 ON table_1.ID = table_2.TransferID
WHERE
    table_2.TransferID IS NULL

第二种情况,

--We want to check `email` against `account` and `accountto`:

SELECT
    table_1.*
FROM
    table_1 
    LEFT JOIN table_2 
        ON table_1.Email = table_2.Account OR table_1.Email = table_2.AccountTo
WHERE
    table_2.TransferID IS NULL

这是数据库小提琴:

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=5acd6dadbe6cb95f5399026f8ea3e052

答案 3 :(得分:0)

您将需要在第二张表中添加时间戳

    select * from table1
    where id not in (
                     Select id from table2 
                     where date >= now() - INTERVAL 1 DAY
                     )