MySQL SQL - 如何获取单元格的特定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19202198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:03:23  来源:igfitidea点击:

SQL - how to get specific value of cell?

mysqlsql

提问by user1386966

Fist time using SQL and already confused. I have a table :

第一次使用 SQL 并且已经感到困惑。我有一张桌子:

A  |  B
--------
a  | 6
b  | 10
c  | 12

I want to filter it by using string comparison, but getting the value of the second column :

我想通过使用字符串比较来过滤它,但获取第二列的值:

myNum = SELECT B WHERE A ='a'

At the end I want the value of myNum to be 6.

最后,我希望 myNum 的值为 6。

Just can't get it right... any help would be very appreciated!

只是无法正确...任何帮助将不胜感激!

回答by Raghvendra Parashar

You need to specify table name.

您需要指定表名。

SELECT B from table_name WHERE A = 'a';

回答by Andy Lester

You need the table name.

您需要表名。

SELECT b FROM tablename WHERE A='a';

SELECT b FROM tablename WHERE A='a';

回答by Laurence

Firstly, you need to include the table name. Secondly, you need to use := for assignment and @ as a prefix to a variable:

首先,您需要包含表名。其次,您需要使用 := 进行赋值,并使用 @ 作为变量的前缀:

Select 
    @myNum := B 
From 
    test 
Where 
    A = 'a';

Example SQLFiddle

Example SQLFiddle

This will also return a result set. If you just want variable assignment, you can use Select ... Into

这也将返回一个结果集。如果你只想变量赋值,你可以使用Select ... Into

Select 
    B 
into 
    @myNum 
From 
    test 
Where 
    A = 'a'

Example SQLFiddle

Example SQLFiddle

These assume your variable is within MySQL, if you are using a different programming language then there will a different method.

这些假设您的变量在 MySQL 中,如果您使用不同的编程语言,那么会有不同的方法。