如何用伪值内连接表

时间:2017-08-05 06:59:15

标签: mysql

根据@scaisEdge here发布的解决方案。我对“内部联接”提出了一个更为深刻的问题。我尝试加入他们的&#p;'值。但是sql说' unknown'

这是他的代码:

 SELECT SUBSTRING(`page_url`,-3) as pid 
  from `prop_log` 
  WHERE `page_url` REGEXP '[0-9]'
  group by SUBSTRING(`page_url`,-3)
  order by count(*)

这是我的:

SELECT prop_sid,SUBSTRING(`page_url`,-3) as pid 
from `prop_log` 
inner join prop_db2 on prop_db2.prop_id=pid
WHERE `page_url` REGEXP '[0-9]'
group by SUBSTRING(`page_url`,-3)
order by count(*) desc
limit 5

PS。 prop_sid来自prop_db2

2 个答案:

答案 0 :(得分:0)

您不能在where(或on)条件

中使用别名
SELECT prop_sid,SUBSTRING(`page_url`,-3) as pid 
from `prop_log` 
inner join prop_db2 on prop_db2.prop_id=SUBSTRING(`page_url`,-3)
WHERE `page_url` REGEXP '[0-9]'
group by SUBSTRING(`page_url`,-3)
order by count(*) desc
limit 5

允许使用别名按顺序在一些mysql版本中按顺序排列但不在哪里..

并且您还应该使用(不在)

列中的(假)聚合函数
SELECT min(prop_sid),SUBSTRING(`page_url`,-3) as pid 
from `prop_log` 
inner join prop_db2 on prop_db2.prop_id=SUBSTRING(`page_url`,-3)
WHERE `page_url` REGEXP '[0-9]'
group by SUBSTRING(`page_url`,-3)
order by count(*) desc
limit 5

或将列添加到您的组中is与查询一致

SELECT prop_sid,SUBSTRING(`page_url`,-3) as pid 
from `prop_log` 
inner join prop_db2 on prop_db2.prop_id=SUBSTRING(`page_url`,-3)
WHERE `page_url` REGEXP '[0-9]'
group by SUBSTRING(`page_url`,-3), prop_sid
order by count(*) desc
limit 5

答案 1 :(得分:0)

SELECT prop_sid, prop_db2.prop_id as pid 
from `prop_log` 
inner join prop_db2 on prop_db2.prop_id=SUBSTRING(`page_url`,-3)
WHERE `page_url` REGEXP '[0-9]'
group by prop_db2.prop_id
order by count(*) desc
limit 5