Unknown column in field list during select query - exception

I used the below query in my Java program
String getQTY="Select
Qty from available_stock WHERE Items='"+ itemName+"';";
ResultSet iset=stat.executeQuery(getQTY);
But it give the error like below
Unknown column Qty in the field list

Try to get DESC from available_stock
DESC available_stock;
Check to see if Qty is present. If it is present, remove the extra ; from your code.
String getQTY="Select Qty from available_stock WHERE Items='"+itemName+"'";

Related

mysql condition 'equal' shows condition 'like' attributes

select * from order_info where order_id = 48;
The mysql command show above is a very simple sentence, but the result below seems not to meet my expectation, it is more similar to like condition.
retrieval result
I don't know if the type of field may affect the result as the field 'order_id' is varchar type. When I change the condition to " where order_id = '48' ", It meets my expectation.
Can anyone tell me why is this case? Why conditon 'equal' show unexpected outcomes?
I expect the result should be empty as the table does not have the record whose order_id is equal to 48.
Consider this -
SELECT CAST('48KKKK' AS INT);
You might expect it to fail , it doesn't and the result is 48. Your query is implicitly converting order id before comparison because you are comparing to an integer so the result of your query is correct and it's a gotcha to remember..

truncated incorrect INTEGER VALUE : 'EN_US'

got this error when I tried to create a table 'top20bypopulation' from a select statement
query :
CREATE table top20bypopulation as
SELECT Name,format(Population,'EN_US') as population FROM country
ORDER BY Population DESC
LIMIT 20;
error :
truncated incorrect INTEGER VALUE : 'EN_US'
But the SELECT runs okey, but when I create table out of it, it gives this error.
I am new to this language, someone please answer how to achieve this.
in your country table Population column is integer and you are trying to get format value
so when we pass two parameter then first one is value and second parameter must be precisioin
if you want to pass locale as parameter then format method be like
format(Population,2,'EN_US')

I need to search record as per status id in some sequence

My sql fiddle : http://sqlfiddle.com/#!9/012138/1
I have following table :
products (TABLE)
id INT(10)
productname VARCHAR(50)
statusid INT(10)
Suppose we have following records:
id productname statusid
5 P1000 2
6 P2000 1
7 P3000 3
Now you can see that status id is 1,2,3. I want to get record as per status id, but status id which we store is in different way so i can not write query to search as per order by statusid directly. Because i need to fetch record as per following status id order
first statusid 3, then status id 1 and then status id 2. How is it possible to write query ?
You can try using FIND_IN_SET
SELECT
*
FROM products
ORDER BY FIND_IN_SET(statusid,'3,1,2')
WORKING DEMO
Note:
Add your custom sequence here FIND_IN_SET(statusid,'3,1,2')
Brief note on FIND_IN_SET:
Description
MySQL FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings. The string list itself is a string contains substrings separated by ‘,’ (comma) character.
This function returns 0 when search string does not exist in the string list and returns NULL if either of the arguments is NULL.
Syntax
FIND_IN_SET (search string, string list)
Arguments
Name Description
search string A string which is to be looked for in following list of arguments.
string list List of strings to be searched if they contain the search string.
Related post
Alternatively you can use FIELD() function to accomplish that
Or use FIELD:
select *
from products
order by field(statusid, 3, 1, 2)
Demo Here

Hive Order by Not working

Query:
Column without function:
SELECT ACCOUNTID from table order by ACCOUNTID;
(Above query works fine in both HIVE & MYSQL)
Column with function:
SELECT concat('test_',ACCOUNTID) from table order by ACCOUNTID;
In mysql , Above query working fine.
In hive, below exception thrown
FAILED: SemanticException [Error 10004]:
Line 1:59 Invalid table alias or column reference 'ACCOUNTID':
(possible column names are: _c0)
Mysql :
Order by working fine with columns involved in functions
Hive :
Order by is not working with columns involved in functions
Temporary Solution :
SELECT concat('test_',ACCOUNTID) as tempColumn from table order by tempcolumn;
Is there any solution available without assigning column as tempcolumn ?
Note:
i want concatenated result alone(test_ACCOUNTID).. without exposing ACCOUNTID as another column
Maybe you can also select the accountid column for sorting.
SELECT accountid,concat('test_',ACCOUNTID) from table order by accountid;
Or, rename "concat('test_', accountid) " as accountid, like this:
SELECT concat('test_',ACCOUNTID) accountid from table order by accountid;
Depends on what you're trying to achieve. If you want your concatenated results to appear in the order of account id then you can expose ACCOUNTID in the projection list
SELECT concat('test_',ACCOUNTID), ACCOUNTID FROM <table> ORDER BY ACCOUNTID
As per my knowledge,its the only way to use alias-name for function expression and use clauses like 'Group By','Order By','Sort By' using that alias-name.

mysql ordering exception

I have a MySQL database and a have a funny question.
I need to order the results of a query by a field which has entries of 1,2,3 or 4, ordered descending but with 4 at the end.
So I wish to have the results in the following order
3's
2's
1's
4's
Is this possible at all?
I know I can do order the result array in php but unfortunately I need to do this in an sql statement.
If the field is an int,
ORDER BY (fieldname = 4) DESC, fieldname DESC
should do the trick.
Here is another cool way
ORDER BY MOD(fieldname,4) DESC,fieldname
If the result is a CHAR(1) then
ORDER BY LOCATE(fieldname,'3214'),fieldname
add this to the order
ORDER BY
CASE field_name WHEN 4 THEN 1
ELSE 2
END
this will return the result of the query order using the value of the field