dongrong9938 2016-07-30 09:04
浏览 72

将数据插入表中并立即选择行的ID号

I have a MYSQL table called 'Buyers'. I am taking form data and inserting it into the table on a page called insert.php.

Now, along with the column names for the form fields, I have an Auto-increment ID column in the table. What I want is; once I send the form data to the table, I want to then execute some SQL to get the ID number for the row into which I just inserted the data into.

E.g, I have the SQL:

INSERT INTO Buyers (name, email, job) VALUES ('$_POST[name]', '$_POST[email]', '$_POST[job]');

This will create a row in the 'Buyers' table with the data from form fields 'name', 'email' and 'job'.

This row will have an ID number generated by Auto Increment.

How, then, can I then select that exact ID number? Is there some way to select the most recent row in the table, since that is the row which contains the ID number I want?

  • 写回答

3条回答 默认 最新

  • dsirr48088 2016-07-30 09:20
    关注
    SELECT x.*
      FROM messages x
      JOIN 
          ( SELECT from_id, MAX(time_sent) time_sent GROUP BY from_id ) y
        ON y.from_id = x.from_id 
       AND y.time_sent = x.time_sent;
    

    The last part of this puzzle is left as an exercise for the reader.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)