我的SQL select语句中的case语句

时间:2018-11-16 03:58:45

标签: mysql

SELECT
column1,
column2,
(CASE WHEN column1=NULL THEN @NEW_column =column2
WHEN column1!=NULL THEN @NEW_column =column1
END) AS NEW_column,
FROM my_table

在上面的select语句中,我正在创建一个名为
的新变量。  NEW_column及其值应基于case语句中的条件。 当我运行此查询值是“ NEW_column”返回为NULL。谁能帮助我解决这个逻辑错误?

2 个答案:

答案 0 :(得分:2)

您需要类似地column1 is nullcolumn1 is not null

DEMO

SELECT
   column1,
   column2,
   CASE WHEN column1 is NULL THEN column2
   WHEN column1 is not NULL THEN column1
   END AS NEW_column,
FROM my_table

输出:

col1    col2    newcol
3       null    3
null    4       4
5      null     5

答案 1 :(得分:0)

这意味着您的条件没有任何价值

有几种方法可以调试SQL语句:

样本

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>

struct Data
{
    std::string name;
    int age;
    char gender;
    std::string comments;
    Data(const std::string& n, int a, char g, const std::string& c) // provide a Constructor
        :name(n), age(a), gender(g), comments(c)
    {}
};

void debugMsg(const std::string& msg)
{
    system("cls");
    std::cout << "\n\n\t\t" << msg << "\n\n";
    Sleep(3000);
}

int main()
{
    std::vector<Data> person; // use std::vector to store the datas
    while (true)  // loop: 1
    {
        system("cls");
        std::cout << "Welcome to the Data Base! \n\n";
        std::cout << "[1] View Person\n";
        std::cout << "[2] Add Person\n";
        std::cout << "[3] Edit Person\n";
        std::cout << "[4] Delete Person\n";
        std::cout << "[5] Exit\n";
        std::cout << "Choose Option: ";  
        int option;  std::cin >> option; 
        switch (option) // use switch to validate the options
        {
        case 1:
        {
            while (true)   // loop - 2 -> case 1
            {
                // if no data available to show -> just break the loop 2 and return to the outer loop(i.e, loop 1) 
                if (person.empty()) { debugMsg("No person available to show ....going to main manu...."); break; }
                // otherwise: displaying all people
                for (std::size_t index = 0; index < person.size(); ++index) 
                    std::cout << index << ".) " << person[index].name << "\n";
                std::cout << "\nEnter number of person you want: ";  
                std::size_t index;  std::cin >> index;
                // if the index is not valid -> just break the loop 2 and return to the outer loop(i.e, loop 1) 
                if (index < 0  || index >= person.size()) { debugMsg("Sorry, wrong index!... returning to the main menu......");  break; }
                system("cls");
                std::cout << "Name: " << person[index].name << std::endl;
                std::cout << "Age: " << person[index].age << std::endl;
                std::cout << "Gender: " << person[index].gender << std::endl;
                std::cout << "Comments: " << person[index].comments << std::endl << std::endl;
                std::cout << "View another person?(y/n): ";      
                char ans;  std::cin >> ans;
                if (ans == 'y') { system("cls");    continue; } // just continue looping
                else if (ans == 'n') { break; }                 // this will break the loop 2 and return to the outer loop(i.e, loop 1)                 
                else { debugMsg("Sorry, wrong option!... returning to the main menu......");  break; }
            }
        } break;
        case 2:
        {
            while (true)   // loop - 3 -> case 2
            {
                system("cls");
                std::string name, comments; int age; char gender;
                std::cout << "Name: ";           std::cin >> name;
                std::cout << "Age: ";            std::cin >> age;
                std::cout << "Gender(M/F/H): ";  std::cin >> gender;
                std::cout << "Comments: ";       std::cin >> comments;
                // simply construct the Data in person vector in place
                person.emplace_back(name, age, gender, comments);
                std::cout << "\n\nAdd another person?(y/n): ";
                char ans;  std::cin >> ans;
                if (ans == 'y') { system("cls"); continue; }
                else if (ans == 'n') { system("cls"); break; } // same as case 1
                else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
            }
        } break;
        case 3: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
        case 4: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
        case 5: return 0; // if its 5, just retun the main
        default: break;
        }
    }
    return 0;
}