开发者

How Select Only One Record in MySQL?

开发者 https://www.devze.com 2023-03-25 05:50 出处:网络
I am doing Win-form project with MySQL. In My Problem is,.. I want to Show very first data in My Project.

I am doing Win-form project with MySQL.

In My Problem is,.. I want to Show very first data in My Project.

I know if the primary key is standard like 1 means its very easy. But in my project that primary key id is changeable one. If ascending or descending is also take all the record and then finely give the result.

I want very first record na?. Is any to Identify the First record Query in MySQL.

My Code -

 connection.Open();
                command.CommandText = "select student_code from attendance_master where subject_code='" + subcode + "' and period_code='" + percode + "' and date='" + date + "开发者_运维百科'";
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    stdcode = Reader[0].ToString();
                }
                connection.Close();

Or other wise can i find the min value of primary id value and select the particular record. I don't the this is a best way. But it reduce unwanted execution of records.

Pls give me an IDEA....


Try using the limit statement, Example:

SELECT * FROM table LIMIT 0, 1


How about this?

SELECT TOP 1 * FROM table  


you could do this

 connection.Open();
 command.CommandText = "select student_code from attendance_master where subject_code='" + subcode + "' and period_code='" + percode + "' and date='" + date + "'";
 Reader = command.ExecuteReader();
 if (Reader.Read())
  {
   stdcode = Reader[0].ToString();
  }
 connection.Close();

The way you use SQL is wide open for SQL injection which is a big security problem!
Better use Parameters in your SQL...


You must sort rows by primary key: http://dev.mysql.com/doc/refman/5.5/en/sorting-rows.html And, then, limit output by LIMIT clause: http://dev.mysql.com/doc/refman/5.5/en/select.html


In Mysql SELECT * FROM table LIMIT 0, 1

IN SQL SELECT TOP 1 * FROM table

0

精彩评论

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