简体   繁体   中英

What is wrong with this mysql query?

Hai guys,

I ve tried a query and i dont know what is wrong with the select statement,when i execute this no records are returned...

It seems rollid=3 is the prob records with rollid 3 are not fetched ,

select * from tbl_emp_personal_master where dEmp_id ='7'


dEmp_id is int

I even tried removing quotes from '7' to 7 it didnt help alt text http://img709.imageshack.us/img709/1901/mysqli.jpg

when i repaired my table repair table tbl_emp_personal_master it started to work

The other answers suggesting:

select * from tbl_emp_personal_master where dEmp_id = 7

should work. If it doesn't, it means that the row doesn't exist in your database. Try:

select * from tbl_emp_personal_master where dEmp_id < 1000

and see if that returns any results.

Let's try a little debugging. You say that

select * from tbl_emp_personal_master where dEmp_id = '7'

doesn't work, nor does:

select * from tbl_emp_personal_master where dEmp_id = 7

Then you should isolate what does work. Try each of the following:

select * from tbl_emp_personal_master where dEmp_id < 10   order by dEmp_id desc
select * from tbl_emp_personal_master where dEmp_id < 100  order by dEmp_id desc
select * from tbl_emp_personal_master where dEmp_id < 1000 order by dEmp_id desc

until you get some output, then have a look at the last few lines of each where you should find employee number 7.

If it's not there, then you have no such employee and that's your problem. If it is there, we're going to need some more investigation since your integer select of 7 should work fine in that case.


Update: Okay, with only 14 records, in the table, show us the output from:

select * from tbl_emp_personal_master order by dEmp_id

and we should be able to help from there.

从7中删除引号。

7 instead of '7'. You're trying to match an int field with a charatcer.

Then in your 'tbl_emp_personal_master' there's no dEmp_id with the value '7'.

select dEmp_id from tbl_emp_personal_master query should tell you whether you have 7 value in the listed values in the result set. Check!

I solved it by repairing my table repair table tbl_emp_personal_master . My query was executed. Thanks for your patience.

My guess is indeed that there is no record having the dEmp_id field set to 7.

  select * from tml_emp_personal_master where dEmp_id between 6 and 8

should allow to check this.

Regarding the enclosure of the field, it depends on your DBMS. Enclosing numeric values in MySQL is perfectly legal and even recommended (cf chapter about SQL injections and numeric values).

Regarding what is wrong , it is generally recommended not to use select * but to list all required fields for performance and readability purposes.

Also, if the query is ran from a programming language, it is strongly adviced to use parameterized queries if available (for instance DBParameter in .Net, PDO in PHP etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM