Please help me to write HQL query for the following SQL query:
SELECT MAX(CAST(SUBSTRING([columnname], 6) AS UNSIGNED))+1
FROM [Tablename]
WHERE DistrCode = [(value)];
I cannot try it myself, but just from searching around I found these probable solutions:
Solution 1
SELECT MAX(CAST(SUBSTRING([columnname], 6) AS UNSIGNED INTEGER))+1
Solution 2
SELECT MAX(CAST(SUBSTRING([columnname], 6) AS INTEGER))+1
Maybe you can try both and report back, which one worked.
Related
i have this database
i want to select options_e if it has options less than 4, or something like this
SELECT * FROM `options` WHERE count(qid) <4
so that it will return the result of options who are less than 4. but on phpmyadmin when i run the query, it says invalid use of group function. why do I get this error? who can i fix this?
You can try like this:
SELECT options_e, options_f, options_p, options_a, options_s FROM `options`
GROUP BY options_e, options_f, options_p, options_a, options_s
HAVING count(qid) < 4
I received wonderful help in answering a question yesterday that lead me to successfully use SELECT COALESCE in a MySQL database to replace a nested excel formula. What i'm now struggling with is using the SELECT COALESCE in conjunction with using the SELECT command in the same query. All my columns are coming from the same table (best_estimate) but what I would like to do is:
SELECT 'OFS_ID' from best_estimate
and also perform the following:
SELECT COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value
FROM best_estimate
so the end result will result in two columns -
OFS_ID and MY_VALUE
I'm just not sure how to join these two different commands - I spent three hours working on it yesterday and I keep getting syntax errors saying that I can't use SELECT in that place in the query.
Thanks in advance for the help!
Have you tried
SELECT OFS_ID,COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value FROM best_estimate
Try this:-
SELECT `OFS_ID`, COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value
FROM best_estimate
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.
So basically, in my PHPMYADMIN I'm toying around with SQL injections so that I can eventually become a pen tester
SELECT *
FROM `ip` as ipstr
WHERE 'id' = $USERINPUT_HERE
ORDER BY
ipstr.id ASC
LIMIT 0 , 30
I'm having difficulty because it says that 'ipstr' is no longer defined when I try to inject it into my phpmyadmin.
I know that if it were a subquery, I would be able to do this. But its not a subquery, so how can I make something like the following work?
SELECT *
FROM `ip` as ipstr
WHERE 'id' = $USERINPUT_HERE
UNION ALL SELECT 1,2,3,4,5
ORDER BY
ipstr.id ASC
LIMIT 0 , 30
Somebody I know is saying it is possible to do this kind of injection, but I cannot figure out how to do it without rewriting the whole query and using subqueries. Thanks.
Let's see. If the string contained ''; delete ip; select * from (select 1 as id) ipstr, then the code might do something you don't expect. (To be honest, the entire statement might get rejected because it is multiple statements, but this is an example.)
The solution to SQL injection is to use parameterized queries. This is not a hard concept. And it is a good habit. You should get into that habit.
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