- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我非常擅长 SQL 语句,希望能得到一点帮助。我发现我需要对其中一些表使用 OUTER 联接,但 MySQL 没有该功能。
SELECT validclick.CampaignName AS CampaignName,
validclick.Website AS Website,
validclick.RevClicks AS RevClicks,
validclick.Revenue AS Revenue,
validclick.TQ AS TQ,
validclick.Source AS Source,
validclick.ScoredClicks AS ScoredClicks,
validclick.Searches AS Searches,
vci.Impressions AS Impressions,
(validclick.RevClicks / validclick.Searches * 100) AS CTR,
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0)) AS Spend,
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0)) AS PaidClicks,
(validclick.Revenue -
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0))) AS Profit,
(validclick.TQ / validclick.ScoredClicks) AS ScoredTQ,
(validclick.TQ / validclick.RevClicks) AS UnscoredTQ,
(validclick.Revenue /
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0))) AS RPI,
(validclick.Revenue / validclick.RevClicks) AS RPC,
((IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0)) /
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0))) AS CPC,
((validclick.Revenue -
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0))) /
(IFNULL(yaho.Spend, 0) + IFNULL(facebook.Spend, 0) + IFNULL(google.Spend, 0)) * 100) AS ROI,
(validclick.RevClicks /
(IFNULL(yaho.PaidClicks, 0) + IFNULL(facebook.PaidClicks, 0) + IFNULL(google.PaidClicks, 0)) * 100) AS Conversion
FROM
(SELECT camp.CampaignName AS CampaignName,
MAX(camp.Website) AS Website,
MAX(camp.Source) AS Source,
SUM(vc.Clicks) AS RevClicks,
SUM(vc.Revenue) AS Revenue,
SUM(vc.TQ) AS TQ,
SUM(vc.ScoredClicks) AS ScoredClicks,
SUM(vc.BiddedSearches) AS Searches
FROM
(SELECT AffID,
MAX(CampaignName) AS CampaignName,
Website,
MAX(Source) AS Source
FROM campaigns
GROUP BY AffID) AS camp
JOIN
(SELECT AffID,
SUM(Clicks) AS Clicks,
SUM(AffiliateRevenue) AS Revenue,
SUM(BiddedSearches) AS BiddedSearches,
SUM(CASE WHEN TQ > 0 THEN (TQ * Clicks) ELSE NULL END) AS TQ,
SUM(CASE WHEN TQ > 0 THEN Clicks ELSE NULL END) AS ScoredClicks
FROM validclickvc
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY AffID) AS vc
ON vc.AffID = camp.AffID
GROUP BY camp.CampaignName) AS validclick
LEFT JOIN
(SELECT CampaignName,
SUM(Spend) AS Spend,
SUM(OutboundClicks) AS PaidClicks
FROM facebookads
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY CampaignName) AS facebook
ON validclick.CampaignName = facebook.CampaignName
LEFT JOIN
(SELECT CampaignName,
SUM(Spend) AS Spend,
SUM(Clicks) AS PaidClicks
FROM yahoo
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY CampaignName) AS yaho
ON validclick.CampaignName = yaho.CampaignName
LEFT JOIN
(SELECT CampaignName,
SUM(Cost) AS Spend,
SUM(Clicks) AS PaidClicks
FROM adwords
WHERE Date BETWEEN '2018-05-06' AND '2018-05-07'
GROUP BY CampaignName) AS google
ON validclick.CampaignName = google.CampaignName
LEFT JOIN
(SELECT camp.CampaignName AS CampaignName,
SUM(vc.Impressions) AS Impressions
FROM
(SELECT AffID,
MAX(CampaignName) AS CampaignName
FROM campaigns
GROUP BY AffID) AS camp
JOIN
(SELECT ty AS AffID,
COUNT(DISTINCT(id)) AS Impressions
FROM validclickimpressions
WHERE ts BETWEEN '2018-05-06 00:00' AND '2018-05-07 23:59'
GROUP BY AffID) AS vc
ON vc.AffID = camp.AffID
GROUP BY camp.CampaignName) AS vci
ON validclick.CampaignName = vci.CampaignName
ORDER BY CampaignName;
我知道这可能不是最有效的 MySQL 语句,但我们很快就会改变事物的结构,所以我没什么大不了的。现在我需要一些方法来使用 OUTER join,或者执行 LEFT JOIN UNTION RIGHT JOIN 但使用别名。我需要完全加入的表(别名)是 vci、facebook、google 和 yaho。关于如何解决这个问题的任何想法?
最佳答案
MySQL 不支持FULL [OUTER] JOIN
。
我们可以使用两个单独的选择来模拟 MySQL 中的完全外部连接,并合并结果。外连接和反连接的模式。
-- outer join (all rows from a along with matching rows from b)
SELECT ... FROM a LEFT JOIN b ON ...
UNION ALL
-- anti-join (rows from b with no matching row in a)
SELECT ... FROM b LEFT JOIN a ON ... WHERE a.notnull_col IS NULL
作为一个简单的演示,两个表a和b:
table a table b
an bn
------- -------
1 2
2 3
3 5
4 7
示例查询
-- left outer join (rows in a with matching rows from b)
SELECT a.an, b.bn
FROM a
LEFT
JOIN b
ON b.bn = a.an
UNION ALL
-- anti-join (rows from b with no match in a)
SELECT a.an, b.bn
FROM b
LEFT
JOIN a
ON a.an = b.bn
WHERE a.an IS NULL
应该返回这样的东西(没有 ORDER BY 子句,行的顺序是不确定的)
an bn
------- -------
1 (NULL)
2 2
3 3
4 (NULL)
(NULL) 5
(NULL) 7
关于MySQL OUTER 加入 AS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223613/
我想对 JOIN 进行特定的排序 SELECT * FROM (lives_in as t1 NATURAL JOIN preferences p1) l1 JOIN (lives_in t2 NAT
我正在努力解决一个查询。并想知道是否有人可以提供帮助。 我有一个标签表(服务请求票)和序列号表 从我的标签中我正在这样做 Select * from tag where tag.created BET
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我有两个表 tbl_user 和 tbl_lastchangepassword,如下所示 表 tbl_user id| name --------- 1 | user1 2 | user2 3 |
我有下一个问题 SELECT i.*, gu.* vs.* FROM common.global_users gu LEFT JOIN common.global_users_perms gup ON
我有一个电影表和一个投票表。用户为他们喜欢的电影投票。我需要显示按电影总票数降序排列的电影列表。我现在所拥有的有点作品。唯一的问题是它不显示 0 票的电影。 SELECT m.name, m.imdb
我有一个由这样的表组成的 mySql 数据库: 我如何(如果可能的话)使用 JOINS 从名称/周期表中获取结果?简单来说,它是如何工作的?我向菜鸟问题道歉。我对此很陌生。任何帮助将不胜感激。 最佳答
我需要查询单元先决条件的自引用关系。 我知道您需要使用两个联接,我是否选择我的列然后将其联接到自身? SELECT u.unit_code, u.name + ' is a prerequisi
我有两个实体,用户和友谊,它们看起来像: public class User { public int UserId { get; set; } (..
假设我有两个表: Table A ProdID | PartNumber | Data... 1 | ABC-a | "Data A" 2 | (null) |
说我有这个数据, (df <- data.frame( col1 = c('My','Your','His','Thir'), col2 = c('Cat','Dog','Fish','Dog')))
我有两个这样的数组,实际上这是从两个不同的服务器检索的 mysql 数据: $array1 = array ( 0 => array ( 'id' => 1, 'n
我的数据库中有以下表格 CREATE TABLE [author_details] ( [_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [name
我正在努力使用一个相当简单的 sql select 语句的 join/where 子句。 我正在尝试从 tb1 中检索产品信息列表,其中 where 条件位于 tbl2 中,但这必须由三个不同的列连接
我正在寻找以下功能: Applicative f => f (f a) -> f a Hoogle给我看join : >:t join join :: Monad m => m (m a) -> m
我有两个“表”,分别是 USER 和 CONGE。在表“CONGE”中,我插入了用户的 ID。但是我不知道如何根据用户的id显示用户的休假。 我想根据id发布“Congé”。 { "conge"
我们有一个具有(简化)结构的文档,如Elasticsearch所示: { _id: ..., patientId: 4711, text: "blue" } { _id: ..., patientId
这两个sql语句有什么区别 a) 从 T1,T2 中选择 *,其中 T1.A=T2.A ; b) 从 T1,T2 中选择 *,其中 T2.A=T1.A ; 在这两种情况下我得到相同的输出,这两种语句之
我想做一个简单的连接,只是比较两个表中的 ID.. 我有我的组表,包含; 身份证 姓名 等.. 我的 GroupMap 表包含; 身份证 组号 元素编号 我的查询采用 GroupMap.ItemID
所以我有一组主要数据,如下所示: value_num code value_letter 1 CDX A 2 DEF B
我是一名优秀的程序员,十分优秀!