› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
m939594960
V2EX  ›  MySQL

SQL 语句求助 我是在编不下去了。。。。

  •  
  •   m939594960 · Apr 28, 2015 · 5496 views
    This topic created in 4179 days ago, the information mentioned may be changed or developed.
    SELECT
        custom_name,
        think_houses.houses_name,
      (SELECT custom_status_status FROM think_custom_status WHERE custom_custom_id=custom_id ORDER BY custom_status_status DESC LIMIT 0,1) as custom_status
    FROM
        think_custom
    INNER JOIN think_houses ON think_custom.houses_houses_id = think_houses.houses_id
    WHERE
        `borker_borker_id` = '4'
    LIMIT 0,
     1
    

    说下 我这个表要干啥。。
    一个经纪人表
    取出 经纪人对应的客户列表 然后 每个客户的状态 与 客户的发生状态的时间 (存于 custom_status表)

    然后还有个筛选功能 客户状态为 4 的所有。。。。

    我感觉一个sql语句实现不了把! 我是编了好半天

    原谅我的低基础。。。

    17 replies  •  2015-04-29 08:46:54 +08:00
    cover
        1
    cover  
       Apr 28, 2015
    虽然没有看懂,但是好像很厉害的样子。。
    没有详细的表结构 也看不出啥,反正先想办法把正确的数据select出来,然后在优化sql语句把。。(比如左右连接)
    我看你的需求好像意思就是
    select custom_status ,custom_id,custom_staus_time from custom_status where broker_id = brokerId and custom_status = 4 ?
    m939594960
        2
    m939594960  
    OP
       Apr 28, 2015
    @cover 感谢你回答! 刚刚脑子没转过来 现在已经大概实现了!!!
    asj
        3
    asj  
       Apr 28, 2015
    你可能需要描述一下custom_status表里的数据。
    是不是一个custom对应多条status记录,分别记录不同时间点的状态?
    还是status里只是记录custom当前的状态,如果这样的话非常简单,三张表join一下就行了
    mcfog
        4
    mcfog  
       Apr 28, 2015
    join 子查询什么的,用超过2个只能说明要么表设计不合适了,要么该冗余一份数据了
    (定时出报表等情况除外)
    staticor
        5
    staticor  
       Apr 28, 2015
    selectc customer, broker, datetime, status
    from (
    select broker, broker_id from b_table
    natural left join (
    select broker_id, c_id, customer, datetime, status
    from cus_table
    )

    where status = 4


    ???
    m939594960
        6
    m939594960  
    OP
       Apr 28, 2015
    @asj custom_status为用户状态表
    custom对应多条status记录,分别记录不同时间点的状态? 是这样的!!
    m939594960
        7
    m939594960  
    OP
       Apr 28, 2015
    @mcfog 效率啥的离我还有点远。。 主要实现功能
    这个是一个房产经纪人的产品

    这个是一个交易列表需要调用的sql

    列表主要包含信息
    楼盘名称 (我放到一个表里)
    经纪人名称 (我放到一个表里)
    交易现在状态(我放到一个表)
    最后状态时间 (我放到交易现在状态表。)


    这个表结构 应该如何优化? 放到一个表里? 在状态改变的时候往主表里写?

    谢谢你了!
    m939594960
        8
    m939594960  
    OP
       Apr 28, 2015
    @mcfog 怎么样去实现是最好的办法?
    iamdbc
        9
    iamdbc  
       Apr 28, 2015
    把你需要联查的数据冗余到现在的表里。
    或者如果经纪人id不是很多,可以判断出来多少个的话,把经纪人id取出来,在关联表里where in。
    一个SQL实现不了就拆开,不然数据大了,联查就是噩梦。
    m939594960
        10
    m939594960  
    OP
       Apr 28, 2015
    @amdbc 嗯谢谢 !
    lujiajing1126
        11
    lujiajing1126  
       Apr 28, 2015
    三个模型,交易,楼盘,经纪人

    交易hasOne楼盘,hasOne经纪人,包括交易现在的状态。。。

    取一个交易。。然后就能取出经纪人和楼盘和现在的状态啊= =。。用ORM关联就好了哈哈哈哈

    也就3条SQL,主键和索引查询要快得多
    mqzhang
        12
    mqzhang  
       Apr 28, 2015 via Android
    什么数据库? 目测需要window函数
    m939594960
        13
    m939594960  
    OP
       Apr 28, 2015
    @lujiajing1126 主要就像使用一条 sql语句。。。。 这种情况是一条sql快 还是分开查快。。。 总结的分开查怪怪的。。。
    @mqzhang mysql php。。。
    rqrq
        14
    rqrq  
       Apr 28, 2015
    页面是怎么显示这些数据的?
    whatisnew
        15
    whatisnew  
       Apr 28, 2015
    EXPLAIN
    m939594960
        16
    m939594960  
    OP
       Apr 29, 2015
    @rqrq 列表啊!
    @whatisnew EXPLAIN 学习中 好复杂。。。
    msg7086
        17
    msg7086  
       Apr 29, 2015
    @m939594960 如果你的程序和数据库在一台机器上,并且查询结果的量不大,那么多数是分开查询效率更高一些。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   800 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 19:37 · PVG 03:37 · LAX 12:37 · JFK 15:37
    ♥ Do have faith in what you're doing.