Fetching a single value from a database [closed]
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;
精彩评论