php 在 MySQL 中选择单行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1412665/
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-25 02:27:18  来源:igfitidea点击:

Selecting a single row in MySQL

phpsqlmysql

提问by aggitan

I am using MySQL, I have a table that has 9 columns. One of them is the primary key.

我正在使用 MySQL,我有一个有 9 列的表。其中之一是主键。

How can I select a single row, by the primary key or column 8 or 4?

如何通过主键或第 8 或 4 列选择单行?

回答by ChristopheD

If I understand your question correctly:

如果我正确理解你的问题:

SELECT * FROM tbl WHERE id = 123 OR colname4 = 'x' OR colname8 = 'y' LIMIT 1

The 'LIMIT' keywordmakes sure there is only one row returned.

“限价”关键字可以确保只有一个返回行。

回答by RedFilter

select *
from MyTable
where MyPrimaryKey = 123

回答by Ian Clelland

Columns in SQL don't have a defined 'order'. Database systems generally keep track of an order for display purposes, but it doesn't make sense to ask a database to select a column by number. You need to know the column's name in order to query its contents.

SQL 中的列没有定义的“顺序”。数据库系统通常为了显示目的而跟踪顺序,但要求数据库按数字选择列是没有意义的。您需要知道列的名称才能查询其内容。

The same thing goes for the primary key (which, incidentally, may not be just a single column). You have to know which column it is, and what that column is named, in order to execute a query.

同样的事情也适用于主键(顺便说一下,它可能不仅仅是一列)。您必须知道它是哪一列,以及该列的名称,以便执行查询。

If you don't know these things, or need to figure them out dynamically, then

如果你不知道这些东西,或者需要动态地弄清楚,那么

DESCRIBE tablename;

will tell you the names of each column, and whether it is part of the primary key or not. It will return a table that you can read, like any other result.

会告诉你每列的名称,以及它是否是主键的一部分。它将返回一个您可以阅读的表,就像任何其他结果一样。