AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / dba / Perguntas / 4024
Accepted
Mokus
Mokus
Asked: 2011-07-22 14:18:08 +0800 CST2011-07-22 14:18:08 +0800 CST 2011-07-22 14:18:08 +0800 CST

Seleção mysql complexa

  • 772

Eu tenho a seguinte tabela,

id | booki_id | bet_id   |      bet  | bettype | line | odds 
1  |   123    |   321    |        1  |    3way |  0,0 |  2.3
2  |   123    |   321    |        2  |    3way |  0,0 |  3.4
3  |   123    |   322    |        1  |    3way |  0,0 |  1.1
4  |   123    |   322    |        2  |    3way |  0,0 |  7.4
5  |   123    |   323    |        1  |    3way |  0,0 |  1.3
6  |   123    |   323    |        2  |    3way |  0,0 |  9.4

Como posso obter o seguinte array? Ou posso ficar com uma seleção?

array
(
[0]=>array
(
   "booki_id"=>123,
   "betid"=>321,
   "bet"=>array([1]=>2.3
                [2]=>3.4
                ),
    "bettype"=>"3way",
    "line"=>0
),
[1]=>array
(
   "booki_id"=>123,
   "betid"=>323,
   "bet"=>array([1]=>1.3
                [2]=>9.4
                ),
    "bettype"=>"3way",
    "line"=>0
)
[2]=>array
(
   "booki_id"=>123,
   "betid"=>322,
   "bet"=>array([1]=>1.1
                [2]=>7.4
                ),
    "bettype"=>"3way",
    "line"=>0
)
)

E é ordenado por bet[1];

ATUALIZADA:

id | booki_id | bet_id   |      bet  | bettype | line | odds 
1  |   123    |   321    |        1  |    3way |  0,0 |  2.3
2  |   123    |   321    |        2  |    3way |  0,0 |  3.4
3  |   123    |   321    |        x  |    3way |  0,0 |  2.4
4  |   123    |   322    |        1  |    3way |  0,0 |  1.1
5  |   123    |   322    |        2  |    3way |  0,0 |  7.4
6  |   123    |   322    |        x  |    3way |  0,0 |  3.4
7  |   123    |   323    |        1  |    3way |  0,0 |  1.3
8  |   123    |   323    |        2  |    3way |  0,0 |  9.4
9  |   123    |   323    |        x  |    3way |  0,0 |  6.4

E às vezes tenho que pedir por bet[2] ou bet[x];

mysql select
  • 1 1 respostas
  • 195 Views

1 respostas

  • Voted
  1. Best Answer
    RolandoMySQLDBA
    2011-07-23T07:21:49+08:002011-07-23T07:21:49+08:00

    Tentei produzir o mesmo pedido e tive alguns problemas, mas aqui estão duas consultas diferentes com as quais você pode começar:

    SELECT
        booki_id,bet_id,bettype,
        group_concat(odds order by bet) betodds,line
    FROM
        bettable
    GROUP BY
        booki_id,bet_id,bettype,line
    ;
    
    SELECT * FROM
    (
        SELECT
            booki_id,bet_id,bettype,
            group_concat(odds order by bet) betodds,line
        FROM
            bettable
        GROUP BY
            booki_id,bet_id,bettype,line
    ) AA
    ORDER BY betodds
    ;
    

    Também criei dados de amostra para testá-los e aqui estão:

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.5.12 MySQL Community Server (GPL)
    
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    
    MySQL (Current test) :: use test
    Database changed
    MySQL (Current test) :: DROP TABLE IF EXISTS bettable;
    Query OK, 0 rows affected (0.03 sec)
    
    MySQL (Current test) :: CREATE TABLE bettable
        -> (
        ->     id       INT NOT NULL AUTO_INCREMENT,
        ->     booki_id INT,
        ->     bet_id   INT,
        ->     bet      INT,
        ->     bettype  VARCHAR(10),
        ->     line     FLOAT,
        ->     odds     FLOAT,
        ->     PRIMARY KEY (id)
        -> );
    Query OK, 0 rows affected (0.05 sec)
    
    MySQL (Current test) :: INSERT INTO bettable (booki_id,bet_id,bet,bettype,line,odds) VALUES
        -> (123,321,1,'3way',0.0,2.3),
        -> (123,321,2,'3way',0.0,3.4),
        -> (123,322,1,'3way',0.0,1.1),
        -> (123,322,2,'3way',0.0,7.4),
        -> (123,323,1,'3way',0.0,1.3),
        -> (123,323,2,'3way',0.0,9.4);
    Query OK, 6 rows affected (0.02 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    
    MySQL (Current test) :: SELECT
        ->     booki_id,bet_id,bettype,
        ->     group_concat(odds order by bet) betodds,line
        -> FROM
        ->     bettable
        -> GROUP BY
        ->     booki_id,bet_id,bettype,line
        -> ;
    +----------+--------+---------+---------+------+
    | booki_id | bet_id | bettype | betodds | line |
    +----------+--------+---------+---------+------+
    |      123 |    321 | 3way    | 2.3,3.4 |    0 |
    |      123 |    322 | 3way    | 1.1,7.4 |    0 |
    |      123 |    323 | 3way    | 1.3,9.4 |    0 |
    +----------+--------+---------+---------+------+
    3 rows in set (0.01 sec)
    
    MySQL (Current test) :: SELECT * FROM
        -> (
        ->     SELECT
        ->         booki_id,bet_id,bettype,
        ->         group_concat(odds order by bet) betodds,line
        ->     FROM
        ->         bettable
        ->     GROUP BY
        ->         booki_id,bet_id,bettype,line
        -> ) AA
        -> ORDER BY betodds
        -> ;
    +----------+--------+---------+---------+------+
    | booki_id | bet_id | bettype | betodds | line |
    +----------+--------+---------+---------+------+
    |      123 |    322 | 3way    | 1.1,7.4 |    0 |
    |      123 |    323 | 3way    | 1.3,9.4 |    0 |
    |      123 |    321 | 3way    | 2.3,3.4 |    0 |
    +----------+--------+---------+---------+------+
    3 rows in set (0.02 sec)
    
    MySQL (Current test) ::
    

    Você terá que brincar com a ordem de todas as linhas a partir daqui.

    De uma chance !!!

    • 2

relate perguntas

  • Existem ferramentas de benchmarking do MySQL? [fechado]

  • Onde posso encontrar o log lento do mysql?

  • Como posso otimizar um mysqldump de um banco de dados grande?

  • Quando é o momento certo para usar o MariaDB em vez do MySQL e por quê?

  • Como um grupo pode rastrear alterações no esquema do banco de dados?

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Como você mysqldump tabela (s) específica (s)?

    • 4 respostas
  • Marko Smith

    Como você mostra o SQL em execução em um banco de dados Oracle?

    • 2 respostas
  • Marko Smith

    Como selecionar a primeira linha de cada grupo?

    • 6 respostas
  • Marko Smith

    Listar os privilégios do banco de dados usando o psql

    • 10 respostas
  • Marko Smith

    Posso ver Consultas Históricas executadas em um banco de dados SQL Server?

    • 6 respostas
  • Marko Smith

    Como uso currval() no PostgreSQL para obter o último id inserido?

    • 10 respostas
  • Marko Smith

    Como executar o psql no Mac OS X?

    • 11 respostas
  • Marko Smith

    Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL?

    • 4 respostas
  • Marko Smith

    Como faço para listar todos os bancos de dados e tabelas usando o psql?

    • 7 respostas
  • Marko Smith

    Passando parâmetros de array para um procedimento armazenado

    • 12 respostas
  • Martin Hope
    Manuel Leduc Restrição exclusiva de várias colunas do PostgreSQL e valores NULL 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison Como você mysqldump tabela (s) específica (s)? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler Quando uma chave primária deve ser declarada sem cluster? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta Listar os privilégios do banco de dados usando o psql 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas Como posso cronometrar consultas SQL usando psql? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas Como faço para listar todos os bancos de dados e tabelas usando o psql? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - Qual é melhor como chave primária? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k Quando devo usar uma restrição exclusiva em vez de um índice exclusivo? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick Como posso otimizar um mysqldump de um banco de dados grande? 2011-01-04 13:13:48 +0800 CST

Hot tag

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve