插入行后获取总行数?

时间:2015-11-05 23:24:16

标签: mysql sql

为超级新手问题道歉,但我很难找到我想做的事情是否可能,即使它看起来很基本。我想在数据库中插入一行,然后获取总行数(不是总影响行数)。我试过了:

{
    "TableNames": [
        "Questions", 
        "Answers",
        "Votes"
    ]
}

但这会产生错误。我也找到并尝试了aws dynamodb list-tables --query 'TableNames[?==`Answers`]' aws dynamodb list-tables --query 'TableNames[?.==`Answers`]' aws dynamodb list-tables --query '[?TableNames[*]==`Answers`]' 函数,就像这样

INSERT INTO apples (type, number) VALUES ('sour', 3) RETURNING COUNT(*);

但这似乎会返回某种受影响的行数。我做错了什么,这可能吗?如果我错过了现有的帖子,感谢和抱歉,但我找到的所有帖子都是为了计算受影响的行,而我只想要总行数。

2 个答案:

答案 0 :(得分:0)

如果你想获得最后输入的行的ID(假设ID是auto_increment),你可以这样做:

<div class="col-md-4">
<div class="event-img-area">
    <img src="images/event-img-pholder.png">
</div>
<div class="event-short-info-area">
    <p class="event-time-card">{{ eventObject.datetime }}<br>
    <span class="event-title-card">{{ eventObject.title }}</span></p><br>
    <span class="event-address-card">{{ eventObject.address.road }}, {{ eventObject.address.city }}, {{ eventObject.address.state }}, {{ eventObject.address.postal }}, {{ eventObject.address.country }}</span>
</div>
<div class="event-tags-area clearfix">
    <div class="hash-card"><span class="hashes">{{ allHashes({ hashes: eventObject.hashes }) }}</span></div>
    <div class="bookmark-card"><a href="#"><i class="fa fa-bookmark-o"></i></a></div>
</div>

https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id

如果您想获得表格中的记录数,请执行以下操作:

insert into apples (type, number) values ('sour', 3);
select last_insert_id();    

如果要查找使用insert / update语句输入或更新的行数,可以执行以下操作:

select count(*) from apples;

http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_row-count

另请参阅http://dev.mysql.com/doc/refman/5.5/en//information-functions.html#function_row-count

答案 1 :(得分:0)

请尝试:

INSERT INTO apples (type, number) VALUES ('sour', 3); SELECT COUNT(*) AS NumberOfApples FROM apples;