i've a table with ID, Name both are String type in ID i'v value like
1.3.6.1,
1.3.6.2,
1.3.6.1.2,
1.3.6.1.3,
1.3.6.1.4,
1.3.6.2.1.
1.3.7.2,
1.3.7.5,
1.3.8.1,
etc
I need to retrieve records like 1.3.6. .. but not like 1.3.6.ANY_NUMBER. ..,
Can u help me to write a Derby query for it
Thanks in advance
Hanks
You wrote the answer yourself
WHERE id LIKE '1.3.6%' AND id NOT LIKE '1.3.6.1%'
Maybe you better don't use LIKE but a simple equals:
SELECT * FROM db WHERE id='1.3.6';
Use this to select data from [column_name] in format up to 3 dots (like 1, 1.1, 1.1.1 but never 1.1.1.1)
SELECT SUBSTRING_INDEX([column_name],".",3) FROM [table_name];
or
Use this to select rows where [column_name] is not longer than 5 chars (1, 1.1, 1.1.1 but never 1.1.1.1)
SELECT [column_name] FROM [table_name] WHERE CHAR_LENGTH([column_name]) < 6
Related
How can I use find_in_set in jpa?
I need to achieve like this
SQL - select * from teacher where find_in_set("5", deptIds) and id = 101
where deptIds have comma separated ids (I know it's bad idea but legacy.)
To do so I had been tried using Criteria but not found any Restrictions that can fulfill find_in_set.
Note - need possible solution with Criteria and Restrictions
criteriaBuilder.function("find_in_set", Boolean.class,
criteriaBuilder.literal(s),
root.get("field"))
Here is an example:
Here is java code snippet.
list.add(cb.greaterThan(cb.function("FIND_IN_SET", Integer.class,
cb.literal(val.toString()), root.get(attributeName)), 0));
select t from Teacher t where find_in_set("5", t.deptIds) = 0 and t.id = 101
I have a PreparedStatement intended to be run both on ORACLE and on MYSQL.
But I cannot figure out how to handle the CAST(NULL AS ...)
On Oracle the following works (but not on Mysql):
SELECT TIMB_INS,
CAST(NULL AS TIMESTAMP) AS TIMB_CLO
FROM TOPS
On Mysql the following works (but not on Oracle):
SELECT TIMB_INS,
CAST(NULL AS DATETIME) AS TIMB_CLO
FROM TOPS
(Please note that the first column selected, "TIMB_INS", returns the correct data type for target database type in both cases, i.e. TIMESTAMP for Oracle and DATETIME for MySql.)
There is a way to put it so that it works for both?
I.E. Can i make it db-indipendent in some way?
Thanks
Marco
Based on the tags I can see you're calling this statement from some java code. There are several ways doing so:
Use the DAO pattern. I.e. for each SQL flavor provide a java file that contains the SQL-s.
Use an ORM like Hibernate or JPA. That will take care of this kind of differences.
As a quick hack, you can edit the SQL manually, like in the snippet below. But then you have to determine somehow if the underlying database is Oracle or MySQL
String SQL_PATTERN = "... CAST(NULL AS %s) AS TIMB_CLO ...";
String SQL = String.format(SQL_PATTERN, isOracle ? "TIMESTAMP" : "DATETIME");
I have an example MyTable with 3 columns - id, common_id, creation_date, where common_id groups entries.
Now I would like to select using CriteriaBuilder all newest entries from each group (that is for each common_id get me latest creation_date).
In SQL the query would look like this:
select * from MyTable where (common_id, creation_date) in (select common_id, max(creation_date) from MyTable group by common_id)
Now I have tried to create the where predicate by writing something like (cb is CriteriaBuilder, root is a Root):
cb.array(root.get('common_id'), cb.max(root.get('creation_date')))
.in(
query.subquery(MyTable.class)
.select(cb.array(root.get('common_id'), cb.max(root.get('creation_date'))))
.groupBy(root.get('common_id')))
But unfortunately cb.array is not an Expression (it's a CompoundSelect), so I cannot use .in() on it.
Thanks for pointers!
could you create it using JPQL? As far that I know, that is not possible.
I looked at the Spect (4.6.16 Subqueries) and it talk about "simples select expression":
simple_select_clause ::= SELECT [DISTINCT] simple_select_expression
I believe that only one return is possible, if you look at the examples there you will not find anything like it.
You will need to use NativeQuery for it.
This is a project on java with openjpa ORM. I have an issue with like selection for type NUMBER(10,2) in Oracle database.
The type of number in JPA is BigDecimal and numbers could be like integer and not integer, for example:
123456
or 123456,01; 123456,15
At presentation layer user has global filter, but not only for this column, where he can choose column and operator (like, equal or between) and define value.
For examaple: column1 like '%56%'
Also numbers at presentation layer has presision 2, so:
123456,00 ->'123456,00'
123456,01 -> '123456,01'
When Oracle database executes like opearaion, nuber is converted ot String type:
123456,00 -> '123456'
and 123456,01 -> '123456,01'
So the result of operation column1 like '%,0%' is only
123456,01 but not both.
I also tried call native SQL function like here but we use OpenJPA 1.2.3 with JPA 1.0 implementation.
Please help me find workaround at my issue.
I don't know if this helps you but on Oracle side you should use:
select *
from your_table
where to_char(your_column,'99999999.99') like '%.0%';
In mysql fulltext search, the match gives some values,I don't know which one is mentioned by that values?
I need the comparsion output in percentage?How can i achieve that?
SELECT id,title,body,MATCH (title,body)
AGAINST ('database') FROM articles WHERE MATCH (title,body)
AGAINST ('database');
Output:
id', 'title', 'body', 'MATCH'
1, 'MYSql Tutorial', 'My Sql is one of the database language', 0.93769526481628
10, 'MySQL vs. YourSQL', 'In the following database comparison ...', 0.93769526481628
6, 'MySQL Tutorial', 'DBMS stands for DataBase ...', 0.92749810218811
I think you mean you want the relevance as well as the result. That would look something like:
SELECT id, title, body, MATCH(title, body) AGAINST ('database') as Relevance FROM `articles` WHERE MATCH(title, body) AGAINST ('database' IN BOOLEAN MODE) ORDER BY `Relevance` DESC
edit I got the question wrong. Here's a list of all relevance's in percentage form:
SELECT a.id, a.title, a.body,
MATCH(a.title) AGAINST ('database') as titleRelevance,
MATCH(a.body) AGAINST ('database') as bodyRelevance,
MATCH(a.title) AGAINST ('database')/c.maxTitleRelevance *100 AS percentageTitleRelevance,
MATCH(a.body) AGAINST ('database')/d.maxBodyRelevance *100 AS percentageBodyRelevance,
c.maxTitleRelevance + d.maxBodyRelevance AS maxTotalRelevance,
(MATCH(a.title) AGAINST ('database')+MATCH(a.body) AGAINST ('database'))/(c.maxTitleRelevance + d.maxBodyRelevance)*100 AS percentageTotalRelevance
FROM `articles` a,
(SELECT MAX(MATCH(b.title) AGAINST('database')) as maxTitleRelevance FROM articles b LIMIT 1) c,
(SELECT MAX(MATCH(b.body) AGAINST('database')) as maxBodyRelevance FROM articles b LIMIT 1) d
WHERE MATCH(a.title, a.body) AGAINST ('database' IN BOOLEAN MODE)
This might be easier to read in the following fiddle. http://sqlfiddle.com/#!2/c7885/14
Full text searches in mysql are flaky at best. If the word database is in at least 50% of rows of the db, it will be ignored as a common stopword if you dont specify boolean mode like in the query above. In terms of how relevance is calculated I'm afraid I cant help you there.