我有一个以下SQL查询
SELECT `User`.`username` , Permalink.perma_link_id, Permalink.locale,
Permalink.title, DATEDIFF( CURDATE( ) , Permalink.created ) AS dtdiff,
`TargetSegment`.segment_text, TargetSegment.source_segment_id,
TargetSegment.perma_link_id ,TargetSegment.created ,
TargetSegment.updated, DATEDIFF( CURDATE( ) ,
TargetSegment.updated ) AS datediff
FROM `users` AS `User`
RIGHT JOIN perma_links AS `PermaLink`
ON ( `PermaLink`.`username` = `User`.`username` )
RIGHT JOIN target_segments AS `TargetSegment`
ON ( `TargetSegment`.`username` = `User`.`username` )
RIGHT JOIN source_segments AS `SourceSegment`
ON ( `SourceSegment`.`source_detail_id` = `PermaLink`.`source_detail_id` )
LEFT JOIN source_details AS `SourceDetail`
ON ( `SourceSegment`.`source_detail_id` = `SourceDetail`.`id` )
WHERE `TargetSegment`.`username` = "xxxx"
AND `TargetSegment`.`segment_text` <> ""
AND `Permalink`.`perma_link_id` = `TargetSegment`.`perma_link_id`
AND `TargetSegment`.`source_segment_id` = `SourceSegment`.`id`
AND `Permalink`.`source_detail_id` = `SourceDetail`.`id`
ORDER BY `TargetSegment`.`updated` DESC
LIMIT 0 , 10
这个SQL正在为我提取正确的结果。我想从哪个表中确定每一行是否来自,具体到哪个结果是由PermaLink表引起的,哪个来自TargetSegment表。
这是可以实现的吗?
答案 0 :(得分:2)
行不是来自一个表或另一个表。每行都来自查询中的所有表。每个表的相应数据连接在一起形成一个长行。
User.username -- from User table
Permalink.perma_link_id -- from Permalink
Permalink.locale -- from Permalink
Permalink.title -- from Permalink
DATEDIFF(CURDATE(), Permalink.created) AS dtdiff -- from Permalink
TargetSegment.segment_text -- from TargetSegment
TargetSegment.source_segment_id -- from TargetSegment
TargetSegment.perma_link_id -- from TargetSegment
TargetSegment.created -- from TargetSegment
TargetSegment.updated -- from TargetSegment
DATEDIFF(CURDATE(), TargetSegment.updated) AS datediff -- from TargetSegment
如果连接条件由于其中一个表中缺少键而失败,并且该表是LEFT联接中的右表或RIGHT JOIN中的左表,那么该表中的所有值都将为NULL。
答案 1 :(得分:0)
我想你想使用别名,如下:
SELECT `User`.`username` , Permalink.perma_link_id AS p_perma_link_id, Permalink.locale AS p_locale,
Permalink.title AS p_title, DATEDIFF( CURDATE( ) , Permalink.created ) AS dtdiff,
`TargetSegment`.segment_text, TargetSegment.source_segment_id AS t_source_segment_id,
TargetSegment.perma_link_id AS t_perma_link ,TargetSegment.created AS t_created,
TargetSegment.updated AS t_updated, DATEDIFF( CURDATE( ) ,
TargetSegment.updated ) AS datediff
FROM `users` AS `User`
RIGHT JOIN perma_links AS `PermaLink`
ON ( `PermaLink`.`username` = `User`.`username` )
RIGHT JOIN target_segments AS `TargetSegment`
ON ( `TargetSegment`.`username` = `User`.`username` )
RIGHT JOIN source_segments AS `SourceSegment`
ON ( `SourceSegment`.`source_detail_id` = `PermaLink`.`source_detail_id` )
LEFT JOIN source_details AS `SourceDetail`
ON ( `SourceSegment`.`source_detail_id` = `SourceDetail`.`id` )
WHERE `TargetSegment`.`username` = "xxxx"
AND `TargetSegment`.`segment_text` <> ""
AND `Permalink`.`perma_link_id` = `TargetSegment`.`perma_link_id`
AND `TargetSegment`.`source_segment_id` = `SourceSegment`.`id`
AND `Permalink`.`source_detail_id` = `SourceDetail`.`id`
ORDER BY `TargetSegment`.`updated` DESC
LIMIT 0 , 10
你会看到以p_为前缀的永久链接列和其他带有t _
的列