开发者

Fetching a single value from a database [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help 开发者_如何学Cclarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

how do i fetch a single value from a database.


Use a SELECT statement, specifying a single column or value, limiting the resultset to one row via a WHERE clause using a unique index, LIMIT clause or only executing the cursor once.


If you want your result set to be only one value, it must have a width and height of 1.

what determines the width? How many columns you select. So make sure you only select 1 column.

what determines height? How many records match your criteria. So make sure your criteria will only match one record (consider using a unique index.)

Putting that together, the basic structure looks like this:

SELECT oneSingleColumn FROM mytable WHERE conditionThatUsesKeysFromUniqueIndex


I guess you are getting multiple values when you are looking for only one value from database. If that is the case then you should change your query to use where clause with primary key or enough conditions so that it narrow down to one row.

Also if you are interested in only one particular field you should use only that column name in select clause.


A basic select statement would be something like this:

SELECT column_name FROM table_name

Here is a great tutorial site for SQL basics: w3schools


Just select the column you want.

SELECT colname FROM mytable WHERE <criteria resulting on one row>;

For example:

SELECT colname FROM mytable WHERE id = 1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜