mysql_insert_id without modifying the db - mysql

I am pretty new to this. Is there anyway to get the last auto increment id without changing the db?
$check = mysql_insert_id();
I tried Max(), but it gave pretty much the same result...

You can use the following :
SELECT Auto_increment
FROM information_schema.tables
WHERE table_name='<your table name>'
AND table_schema = DATABASE();

mysql_insert_id() is the correct Method. Max() does only return the highest id value of your database table, but that dont have to be the latest inserted id. You understand?
Regards

Try something like:
SELECT id FROM mytable ORDER BY id DESC LIMIT 1 FOR UPDATE
The FOR UPDATE is because I'm guessing you want to follow this up with an INSERT. If this is the case, do it in the same transaction and I would recommend specifying an explicit value for id rather than relying on AUTOINCREMENT.

Related

Querying mysql workbench for specific attribute

in mysql workbench, I want to run:
SELECT * FROM testdatabase.testTable;
But I want to restrict this to entries in the table that only have a specific attribute value, like "userId"=123
How would I do this?
Thank you.
Just use a WHERE clause:
SELECT * FROM testdatabase.testTable WHERE userId = 123;
This will select all rows from your table that have the userId 123.

How to replace value of column with MySQL Query

I try to make some changes to my MySQL database, and I need to change values of this col name "id_lang" which values now is "6" to change to value "2".
This col is found in many tables, it will be great to have a single query which will to this for all DB at once.
by now i found this query,
SELECT REPLACE(yourcolumn,'ValueInTheColumnTobeReplaced', 'NewValue') as replacedColumnName FROM yourtable
PS: I use PHPMYADMIN
but i cant make it work... PLease help me!!!
You have to use an Update query.
But you can not do this in one query for all tables.
Update yourTable set id_lang=2 where id_lang=6
Use this query
UPDATE yourTable SET id_lang=2 WHERE id_lang=6;

how to "ident_current" in mysql?

I use this code in MSSQL:
SELECT IDENT_CURRENT('customers');
When I try it in MySQL it doesn't work. I looked for answers on the net, but I could not find anything that worked for me. What's the MySQL equivalent for the above TSQL?
I think you are looking for this:
SELECT LAST_INSERT_ID('customers');
But LAST_INSERT_ID() not in all cases true, so better use:
SELECT MAX('id') FROM customers;
in MySQL, you can use LAST_INSERT_ID(expr)
LAST_INSERT_ID(expr)
I donĀ“t know if you still need it but it may help someone. :)
I was facing the same problem of needing to get the next id (auto_increment number) from my table without having to add a new row and without using MAX() or LAST_INSERT_ID() because it would only return the last visible record and not the real next auto_increment.
The solution I found was to get the auto_increment from the table_schema, like this:
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE (TABLE_NAME = 'your_table')
Hope it help someone, like it helped me.
*Sorry the bad english, I'm from Brasil.
SELECT LAST_INSERT_ID();
To learn more about this function: http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
In order for this to work as intended, the 'ID' column must be set to "UNIQUE".
If you are using some sort of Identity(incrementing IDs) column the following should work;
Select top 1 ID from TBLNAME
order by ID DESC

Determine table existence

I got this piece of code from Mysql 4th edition to check table existence
SELECT * FROM table WHERE FALSE;
I cant quite under stand the where clause.
If im not mistaken there is no boolean type in mysql, so basically it been cast to
SELECT * FROM table WHERE 0;
And should it be a column on a where clause?
SELECT * FROM table WHERE column = false;
Any explaination greatly appreciated.
No it doesn't have to have column as operand :)
When you open mysql select manual than expressions you would find there simple_expr what should mean conditions like WHERE 1 (about booleans) but I understand it can be confusing (and it's rare to use conditions without columns).
When you do SELECT * FROM table WHERE FALSE; it's basically this:
if there's table `table`
return 0 rows (minimal database overhead) and valid resource
else
return false
Just take a look on return values from mysql_query.
I am assuming that MySQL 4.x supports ANSI. If so, you could try the following:
SELECT count(*)
FROM information_schema.tables
WHERE table_schema = <schema-or-db-name>
AND table_name = <table-or-view-name>
Checking on a Table's Existence
$sql="SELECT * FROM $table";
$result=mysql_query($sql);
if (!$result)
{
// table does not exist
}
To answer your question:
The where clause is used to retrieve the value of a column in the given table. For example, if your table consists of two columns (user_id, user_name), your query might look something like the following:
$sql="SELECT * FROM $table where user_id = 1";
Lastly, you may read more about the where clause at this link

MYSQL: select row where field is smaller than other

this is my mysql query in php:
mysql_query("SELECT * FROM tbl WHERE logins < limit")
the mysql_affected_rows() returns -1. why this simple query doesnt work? field and table names are correct i've double checked
Try quoting your column names with backticks:
mysql_query("SELECT * FROM tbl WHERE `logins` < `limit`")
You've got a column name same as a reserved MySQL keyword (limit).
You can't use mysql_affected_rows for SELECT queries, as per the PHP Manual:
mysql_affected_rows
Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query
Use mysql_num_rows instead.
Also, as p.campbell pointed out, don't forget to backquote the name of your 'limit' column, since you use a reserved work as the column name.