dongqijuan3786 2015-09-04 12:42
浏览 58
已采纳

(MYSQL)选择具有特定参数的最后一行

I'm having trouble with a PHP project I was given. More particularly the MYSQL database is troubling me.. Here is the idea of the whole thing:

  • I have made a database already with 3 tables - T1, T2, Log. T1 stores name and code of a employer; T1 stores name and code of a worker; Log stores ID (auto incremented), employer name, worker name, start date and end date (of work).

  • I made a form with two fields - Code for Employer and Code for Worker. My PHP code checks the codes input in the fields (checks if they are empty and then checks if they match the information in T1 and T2). Then with a Submit button all data is sent and stored into Log Table. But I had to add two radio buttons in there: one for the Worker start time and one for his end time. When pressed the Start radio button, the sql request does the following - inserts name of employer, name of worker and start the start date. This part here works flawlessly. My End Date column is set to place current date upon update.

If the End radio button is pressed, I want the following to happen: find the LAST row with the selected in form names for emp and worker (they input codes in the form which are checked and the actual names are stored as variables). So is it possible to make something like:

SELECT ID FROM Log ORDER BY ID DESC LIMIT 1
WHERE Worker_Name='$nameW' and Employer_Name='$nameE'
  • $nameW is the name of the worker and $nameE is the name of the Employer.

I say something like it, because it gives me an error in the whole WHERE part and does not accept my query..

Any help would be appreciated. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • doudou348131346 2015-09-04 12:48
    关注

    Your query is not correct you have to put the order by and the limit part at the end (i.e. after the where condition). The correct query for your situation will be:

    SELECT ID FROM Log 
    WHERE Worker_Name='$nameW' and Employer_Name='$nameE' ORDER BY ID DESC LIMIT 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果