how to "ident_current" in mysql? - 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

Related

SQL Renaming NULL with string

I have looked through questions here and have not been able to find exactly what I am looking for.
How would I create a query in mysql that would return missing data in field Name in database Demo as a string (such as 'Blank') instead of NULL?
I really appreciate your help!
SELECT IFNULL(fieldname, 'Blank') FROM tablename
or
SELECT COALESCE(fieldname, 'Blank') FROM tablename
Yes, in MySQL you can use IFNULL, as in:
SELECT IFNULL(fieldname, 'Blank') as fieldname, ...
I'm not really sure what you're aiming at, but maybe you look for something like this?
SELECT IF(ISNULL(a.yourfield),'Blank', a.yourfield)
FROM yourtable A;
ISNULL() returns 1 if the argument is NULL, otherwise it returns 0.

MySQL AND & OR operator

What is an operator in MySQL queries that covers AND and OR. So if I wanted to select a row where value1=lol OR value2=loly or both do.
this will look for one of the values or both.
SELECT * FROM my_table WHERE your_column IN ( 'lol','loly' )
With a query with the OR it'll work:
SELECT * FROM my_table WHERE my_field = 'lol' OR my_field = 'loly'
it'll give you all the results with the first or the second
As an extra, I think you could be looking for WHERE with regular expressions: Check this manual page: https://dev.mysql.com/doc/refman/5.1/en/regexp.html
Sorry, this wasn't a very good question.
I now understand that the OR operator will work with either condition met, or both.
AND only works with both.
XOR works with either one but not both.
Credit goes to the comments replying to my question.

How to do MySQL + Substring? + replace?

I am not very good with SQL and would like to be able to become better.
I am having some trouble trying to preform a certain table manipulation.
I would like to be able to select the substring out of the ProgUID column below
something like...
SUBSTRING(table.ProgUID,3,12);
which would give me CAMVE-9701 for the ProgUID P-CAMVE-9701-1 (removing the P- from the beginning and the -1 from the end), and then insert the substring into that rows UID.
I assume this should be fairly easy, and I have been trying to figure it out but havent had much luck.
If there is a better approach please let me know!
Thanks in advance for your thoughts / help!
use UPDATE statement
UPDATE tableName
SET UID = SUBSTRING(ProgUID,3,12)
See SQLFiddle Demo
If the portion you want is always 12 characters, then
UPDATE table
SET UID = SUBSTRING(ProgUID, 3, 12)
otherwise
UPDATE table
SET UID = SUBSTRING(ProgUID, 3, LENGTH(ProgUID)-2)

mysql_insert_id without modifying the db

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.

mySql new rows can only be searched with LIKE, has anyone had this before?

A bit of background
I'm running mySQL on a mac
I have a few databases setup that have been working okay
I have recently created a new table from a sqlDump from another server.
I am having an issue with new rows that equal = a value that I know exists
e.g. Table setup
id=1 name='dave' - already exists in database
id=2 name='john' - I add a new row
Following are the sql I tried with results...
Select * from tablename where id=1 -- I get the correct Dave result.
Select * from tablename where `name` = 'dave' -- I get the correct Dave result.
Select * from tablename where id=2 -- I get the correct John result.
Select * from tablename where `name` like 'joh%' -- I get the correct John result.
Select * from tablename where `name` = 'john' -- (No result!) eek!
Anyone seen this before? it's in the database but a direct match on the name field is not yielding a result.
Thanks in advance
M
One possibility: there could be a trailing space after 'john' in the name column.
One way to check that:
select `name`,char_length(`name`), char_length('john')
from tablename
where id = 2
An easy way to not have to deal with that problem would be to trim your input (if you don't expect to ever have preceding or trailing white space.
In that case you could have a query like:
Select * from tablename where trim(`name`) = trim('john')
I agree with the comments on your question, that it is most likely a hidden space or something similar. If you include the column definitions so we can check the data that your using with the types we could help more. Remove the entry and and retry with a different name other than john and see if you can replicate the problem.