我有一个像下面这样的查询返回我期望的正确行数.(它必须匹配类似的查询,该查询返回相同的数据集,但具有与其他相关表不同的关联信息.
SELECT *
FROM LK
INNER JOIN STC ON LK.V = STC.VI
LEFT OUTER JOIN BC ON LK.BC = BC.ID
LEFT OUTER JOIN K AS LT ON ISNULL(BC.ZA, LK.VH) = LT.KNN
WHERE
LT.KI IS NOT NULL AND LT.KS = 0
OR
LT.KI IS NULL
ORDER BY
LK.Z
Run Code Online (Sandbox Code Playgroud)
但是,只要我添加其他内部联接,我实际上会获得更多行.我认为内连接只在连接的两边找到信息时返回行,所以我希望得到相同或更少的行.但我的两倍左右.
例如:
SELECT *
FROM LK
INNER JOIN STC ON LK.V = STC.VI
INNER JOIN VK ON LK.V = VK.ID
INNER JOIN K AS A ON VK.AIN = A.KNN
LEFT OUTER JOIN BC …Run Code Online (Sandbox Code Playgroud) 以下哪个查询更好...这只是一个例子,有很多情况,我希望显示用户名而不是UserID
Select EmailDate, B.EmployeeName as [UserName], EmailSubject
from Trn_Misc_Email as A
inner join
Mst_Users as B on A.CreatedUserID = B.EmployeeLoginName
Run Code Online (Sandbox Code Playgroud)
要么
Select EmailDate, GetUserName(CreatedUserID) as [UserName], EmailSubject
from Trn_Misc_Email
Run Code Online (Sandbox Code Playgroud)
如果使用First没有性能优势,我宁愿使用第二个...我会在用户表中有大约2000条记录,在电子邮件表中有100k条记录...
谢谢
sql-server performance sql-server-2005 inner-join user-defined-functions
我有两张桌子,如下所示
location_distance
----------------------------------------------
id | fromLocid | toLocid | distance
----------------------------------------------
1 | 3 | 5 | 70
2 | 6 | 8 | 15
3 | 2 | 4 | 63
...
Run Code Online (Sandbox Code Playgroud)
other_table
--------------------------------------------
Id | fromLocid | toLocid | otherdata
--------------------------------------------
12 | 5 | 3 | xxxx
22 | 2 | 4 | xxxx
56 | 8 | 6 | xxxx
78 | 3 | 5 | xxxx
Run Code Online (Sandbox Code Playgroud)
我想检索b/w中每行的other_table中的位置的距离.这是我尝试过的
SELECT ot.*, ld.distance FROM other_table AS ot
INNER …Run Code Online (Sandbox Code Playgroud) 我有两张桌子; songs48 959 281行,popular_songs5 721 117行.
这是两个表的结构:
CREATE TABLE songs (
songId BIGINT(20) NOT NULL,
songName VARCHAR(1000) NOT NULL,
songDuration BIGINT(20) NOT NULL,
songPreview VARCHAR(1000) NOT NULL,
PRIMARY KEY (songId),
INDEX (songDuration)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8
DEFAULT COLLATE utf8_general_ci;
CREATE TABLE popular_songs (
storeFrontId BIGINT(20) NOT NULL,
genreId BIGINT(20) NOT NULL,
songId BIGINT(20) NOT NULL,
songRank INT(11) NOT NULL,
INDEX (storeFrontId),
INDEX (genreId),
INDEX (songId),
INDEX (songRank),
CONSTRAINT popular_song UNIQUE (storeFrontId, genreId, songId), …Run Code Online (Sandbox Code Playgroud) 我有一个简单的模型
class User
has_many :logs
class Logs
Run Code Online (Sandbox Code Playgroud)
通过外键logs.user_id以通常的方式相关.我正在尝试使用Arel执行以下操作,并且根据Arel doc它应该可以工作.
u_t = Arel::Table::new :users
l_t = Arel::Table::new :logs
counts = l_t.
group(l_t[:user_id]).
project(
l_t[:user_id].as("user_id"),
l_t[:user_id].count.as("count_all")
)
l_t.joins(counts).on(l_t[:id].eq(counts[:user_id]))
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到了错误
TypeError: Cannot visit Arel::SelectManager
Run Code Online (Sandbox Code Playgroud)
然而,Arel的作者明确暗示 Arel可以做这种事情.
请不要写关于如何使用原始sql,另一种类型的Arel查询等实现相同查询的响应.这是我感兴趣的模式,而不是此查询的特定结果.
我遇到了MySQL(innoDB)5.0的严重问题.
使用非常意外的查询计划执行非常简单的SQL查询.
查询:
SELECT
SQL_NO_CACHE
mbCategory.*
FROM
MBCategory mbCategory
INNER JOIN ResourcePermission as rp
ON rp.primKey = mbCategory.categoryId
where mbCategory.groupId = 12345 AND mbCategory.parentCategoryId = 0
limit 20;
Run Code Online (Sandbox Code Playgroud)
MBCategory - 包含216583行
ResourcePermission - 包含3098354行.
在MBCategory中我有多个索引(列在索引中排序):
Primary (categoryId)
A (groupId,parentCategoryId,categoryId)
B (groupId,parentCategoryId)
Run Code Online (Sandbox Code Playgroud)
在ResourcePermission中,我有多个索引(列在索引中排序):
Primary - on some column
A (primKey).
Run Code Online (Sandbox Code Playgroud)
当我查看查询计划时,Mysql更改表序列并首先从ResourcePermission中选择行,然后它加入MBCategory表(疯狂的想法)并且需要很长时间.所以我添加STRAIGHT_JOIN了强制innodb引擎使用正确的表序列:
SELECT
STRAIGHT_JOIN SQL_NO_CACHE
mbCategory.*
FROM
MBCategory
mbCategory
INNER JOIN ResourcePermission as rp
ON rp.primKey = mbCategory.categoryId
where mbCategory.groupId = 12345 AND mbCategory.parentCategoryId = 0
limit 20;
Run Code Online (Sandbox Code Playgroud)
但是这里的第二个问题是材料:在我看来,mysql应该index …
在这篇维基百科文章SQL join之后,我希望能够清楚地了解如何与data.table建立联接.在这个过程中,我们可能在加入NAs时发现了一个错误.以wiki为例:
R) X = data.table(name=c("Raf","Jon","Ste","Rob","Smi","Joh"),depID=c(31,33,33,34,34,NA),key="depID")
R) Y = data.table(depID=c(31,33,34,35),depName=c("Sal","Eng","Cle","Mar"),key="depID")
R) X
name depID
1: Joh NA
2: Raf 31
3: Jon 33
4: Ste 33
5: Rob 34
6: Smi 34
R) Y
depID depName
1: 31 Sal
2: 33 Eng
3: 34 Cle
4: 35 Mar
Run Code Online (Sandbox Code Playgroud)
LEFT OUTER JOIN
R) merge.data.frame(X,Y,all.x=TRUE)
depID name depName
1 31 Raf Sal
2 33 Jon Eng
3 33 Ste Eng
4 34 Rob Cle
5 34 Smi Cle
6 NA …Run Code Online (Sandbox Code Playgroud) 我试图在mysql数据库中区分两行.
我有这个表包含ID,公里,日期,car_id,car_driver等...
因为我不总是按正确的顺序输入表中的信息,我可能会得到这样的信息:
ID | Kilometers | date | car_id | car_driver | ...
1 | 100 | 2012-05-04 | 1 | 1
2 | 200 | 2012-05-08 | 1 | 1
3 | 1000 | 2012-05-25 | 1 | 1
4 | 600 | 2012-05-16 | 1 | 1
Run Code Online (Sandbox Code Playgroud)
使用select语句,我能够正确地对表进行排序:
SELECT * FROM mytable ORDER BY car_driver ASC, car_id ASC, date ASC
Run Code Online (Sandbox Code Playgroud)
我会得到这个:
ID | Kilometers | date | car_id | car_driver | ...
1 | 100 | 2012-05-04 …Run Code Online (Sandbox Code Playgroud) 我有2个表说A和B,我想对它们进行连接.
表A将始终包含记录.
当表B中有行时,我希望查询转换表A和表B匹配的所有行.(即表现得像内部联接)
但是,如果表B为空,我想从表A返回所有内容.
这可以在1个查询中执行吗?
谢谢.
我有2亿条记录的表Foo和1000条记录的表格栏,它们是多对一连接的.列Foo.someTime和Bar.someField有索引.同样在Bar 900中,记录的某些字段为1,100,其中某些字段为2.
(1)此查询立即执行:
mysql> select * from Foo f inner join Bar b on f.table_id = b.table_id where f.someTime between '2008-08-14' and '2018-08-14' and b.someField = 1 limit 20;
...
20 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
(2)这个只需要永远(唯一的变化是b.someField = 2):
mysql> select * from Foo f inner join Bar b on f.table_id = b.table_id where f.someTime between '2008-08-14' and '2018-08-14' and b.someField = 2 limit 20;
Run Code Online (Sandbox Code Playgroud)
(3)但是如果我在某个时间删除where子句而不是立即执行:
mysql> select * from Foo f inner join Bar b on f.table_id = b.table_id …Run Code Online (Sandbox Code Playgroud) inner-join ×10
mysql ×5
sql ×3
indexing ×2
sql-server ×2
arel ×1
data.table ×1
join ×1
large-data ×1
left-join ×1
outer-join ×1
performance ×1
r ×1
range ×1
select ×1
subquery ×1
t-sql ×1