简体   繁体   English

SELECT中的MySQL CASE语句

[英]MySQL CASE Statement in SELECT

I have a MySQL query question. 我有一个MySQL查询问题。 I built a table that keeps track of high school football scores. 我建立了一张表格,记录高中足球的得分。 It looks like this: 看起来像这样:

CREATE TABLE `games` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `home_team` int(11) NOT NULL,
  `visitor_team` int(11) NOT NULL,
  `home_score` tinyint(4) NOT NULL,
  `visitor_score` tinyint(4) NOT NULL,
  `quarter` tinyint(4) NOT NULL,
  `week` tinyint(2) NOT NULL,
  `game_date` date NOT NULL,
  `game_time` time NOT NULL,
  `complete` tinyint(4) NOT NULL DEFAULT '0',
  `stadium_id` int(11) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=151;

The query that I am doing uses a team's id to compare with. 我正在执行的查询使用团队ID进行比较。 I want to retrieve the id values of the teams that the given team id defeated only. 我想检索仅击败给定团队ID的团队的ID值。 Here is my query so far. 到目前为止,这是我的查询。

SELECT 
    CASE 
        WHEN games.home_team != ? AND (games.home_score > games.visitor_score) THEN games.home_team 
        WHEN games.visitor_team != ? AND (games.home_score < games.visitor_score) THEN games.visitor_team 
    END AS id, teams.class_id, classes.name 
FROM games 
    INNER JOIN teams ON id = teams.id 
    INNER JOIN classes ON teams.class_id = class.id
    WHERE games.complete = 1

When I run this query in PHP MyAdmin I get the following error: 当我在PHP MyAdmin中运行此查询时,出现以下错误:

#1052 - Column 'id' in on clause is ambiguous

I am using PDO's execute() so the ?'s represent team id values. 我正在使用PDO的execute(),所以?代表团队ID值。 I figured since I assigned the result of my CASE statements to id, then I could use it to join the teams table. 自从我将CASE语句的结果分配给id之后,我就知道了,然后可以使用它来加入teams表。 Can anybody point me in the right direction to fix this error? 有人可以指出正确的方向来纠正此错误吗? Thank you in advance. 先感谢您。

You need to use either the table name in front of columns present in multiple tables, or use an alias: 您需要使用多个表中存在的列前面的表名,或者使用别名:

INNER JOIN teams ON games.id = teams.id 

Edit: Having said that, it seems that it is in fact a column alias, which you can't do as is: 编辑:话虽如此,看来它实际上是列别名,您不能原样这样做:

INNER JOIN teams ON
CASE 
    WHEN games.home_team != ? AND (games.home_score > games.visitor_score) THEN games.home_team 
    WHEN games.visitor_team != ? AND (games.home_score < games.visitor_score) THEN games.visitor_team 
END  = teams.id 

You could also use a subquery first, then match the results without the extra case statement. 您也可以先使用子查询,然后在不使用多余case语句的情况下匹配结果。

Select
    sub.id,
    teams.class_id,
    classes.name
from
    (
        SELECT 
            CASE 
            WHEN games.home_team != ? AND (games.home_score > games.visitor_score) THEN games.home_team 
            WHEN games.visitor_team != ? AND (games.home_score < games.visitor_score) THEN games.visitor_team 
            END AS id
        FROM 
            games
        WHERE 
            games.complete = 1
    ) sub
    INNER JOIN teams ON sub.id = teams.id 
    INNER JOIN classes ON teams.class_id = class.id

I made a simple function to make that. 我做了一个简单的功能。 It´s working. 工作正常。

// get variable
$area = $_GET['area'];

// function
function cases($a) {

    $sel_dash = mysql_query("SELECT * FROM sys_dash WHERE area = '$a'");
    $dados = mysql_fetch_assoc($sel_dash);
    $page = $dados['page'];

    return $page;
}

// call function with the variable
$page = cases($area);

To test if the value that you get it´s correct you can change the "return" to "echo" and the variable will be write in the code. 要测试所获得的值是否正确,可以将“ return”更改为“ echo”,变量将被写入代码中。

The table that i use is: 我使用的表是:

CREATE TABLE `sys_dash` (
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `area` VARCHAR(250) NOT NULL,
    `page` VARCHAR(250) NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT

I hope i help ! 希望我能帮忙!

It´s note CASE class for PHP but have the same function that i need. 注意PHP的CASE类,但是具有我需要的相同功能。

Thanks 谢谢

Doniani 多尼尼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM