http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3571|回复: 5

[其他] 不知这个sql还能不能进一步优化

[复制链接]
发表于 2013-1-8 16:01:25 | 显示全部楼层 |阅读模式
[code=sql]SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount FROM gw_bus_stop b1 inner join
gw_bus_stop b2 on b1.lineid=b2.lineid AND b1.stoporder<b2.stoporder AND b1.title_sc='牛头角' AND b2.title_sc='利景'[/code]



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-1-8 16:15:04 | 显示全部楼层
首先你的语句都不对吧
[code=sql]    SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount FROM gw_bus_stop b1 inner join
    gw_bus_stop b2 on b1.lineid=b2.lineid AND b1.stoporder<b2.stoporder AND b1.title_sc='牛头角' AND b2.title_sc='利景'[/code]
b2 b1是同一个表b1.lineid=b2.lineid 其它条件应该都不会成立了吧比如 b1.title_sc='牛头角' AND b2.title_sc='利景'
title_sc这一个字段怎么会有两个值呢?
应该这样写吧
[code=sql]SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount
FROM
gw_bus_stop b1 inner join
gw_bus_stop b2
on b1.lineid=b2.lineid
WHERE b1.stoporder<b2.stoporder AND b1.title_sc='牛头角' AND b2.title_sc='利景'[/code]
当然也可以这样写
[code=sql]SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount
FROM
gw_bus_stop b1 ,
gw_bus_stop b2
WHERE b1.stoporder<b2.stoporder AND b1.title_sc='牛头角' AND b2.title_sc='利景'[/code]
 楼主| 发表于 2013-1-9 08:39:31 | 显示全部楼层
sql语句没有问题的,其实是巴士的站点表。我的lineid字段不是pk,是巴士线路,一个lineid下多个title_sc,要查询的两个站点是否在同一条线路下。
sid lineid stoporder  title_sc       .....
1     1         1        站点一   
2     1         2        站点二
3     1         3        站点三            
你看我的这条sql语句能否查出站点一到站点三的结果来?
lineid=1;stoporder>1;title_sc不同。
SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount FROM gw_bus_stop b1 inner join
gw_bus_stop b2 on b1.lineid=b2.lineid AND b1.stoporder<b2.stoporder AND b1.title_sc='站点一' AND b2.title_sc='站点三'。
当然,或许是我的sql水平太低了。理解不对,希望苏飞大哥指正。谢谢。
发表于 2013-1-9 10:13:23 | 显示全部楼层
哦我以为lineid 是主键
你试试我的这个行不[code=sql]SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount
FROM
gw_bus_stop b1 ,
gw_bus_stop b2
WHERE b1.stoporder<b2.stoporder AND b1.title_sc='牛头角' AND b2.title_sc='利景'[/code]
然后再看看那个快
发表于 2013-1-9 10:13:49 | 显示全部楼层
哦我以为lineid 是主键
你试试我的这个行不[code=sql]SELECT b1.title_sc AS origin ,b1.lineid ,b2.title_sc AS destination,b2.stoporder-b1.stoporder AS stopcount
FROM
gw_bus_stop b1 ,
gw_bus_stop b2
WHERE b1.stoporder<b2.stoporder AND b1.title_sc='牛头角' AND b2.title_sc='利景'[/code]
然后再看看那个快
 楼主| 发表于 2013-1-9 11:11:44 | 显示全部楼层
admin 发表于 2013-1-9 10:13
哦我以为lineid 是主键
你试试我的这个行不
然后再看看那个快

行,我最开始就是使用的这个where,之后网上找找说where最终还是会被解释成join,所以就改了下,但基本差不多,可能是数据比较少吧。没什么体现。哈哈
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-19 07:10

© 2014-2021

快速回复 返回顶部 返回列表