I have to execute a query where there are multiple attributes in where clause and we have multiple such requests. eg. we have to execute below query
Select *
from Equipments
where equipId=?
and equipType=?
I have 1000s of set of these values like (1000,wireless),(2000,wire) . . . .
Which means i want to execute multiple select statements like
Select *
from Equipments
where equipId=1000
and equipType='wireless'
Select *
from Equipments
where equipId=2000
and equipType='wire'
how can i run this select statement in bulk and get the list of java Object mapped to Equipments table, instead of making one select at a time and going in loop .
While doing research on this, i found this and modified my query accordingly and i am able to do multiple select.
There is one limitation still left that in the 'in' clause we have a limitation on number of parameters being passed. If some someone know of that solution please comment
Related
Suppose I have a table with columns 'name', 'age', 'city', 'country'. I would like to expose the database query interface to users, that is, they should be able to perform all kinds of queries that sql let's us do.
The only way I can think of doing this is to have a row for each column where each row is of the form:
column name | operator | value
An example query in an activity would be:
name | = | Bob
age | > | 25
Then with that information I could have a method perform the query and return the result.
For this simple example it would work. But there are more interesting things one can ask sql and this approach would fail at a lot those queries.
What can I do about this?
First of all, you need to think what operations are you going to allow your users. The most common SQL queries are SELECT, INSERT, UPDATE, DELETE. Once you have the list of operations that you are going to provide, you need to think of the parameters that the user can select while making any of these queries. For example, if the user wants to fetch (SELECT) some data, what can he provide as input, in your case the age of the person. Similar case for the other queries.
So once you have the above information, you will need to convert it into a query. This can be done by creating a utility or helper class which takes into account the users query as well as parameters and forms the SQL query which you can execute on your database.
For example, lets take the StackOverflow Jobs page (https://stackoverflow.com/jobs?med=site-ui&ref=jobs-tab). Here you can see that the user can fetch data based on multiple parameters which are keyword, location, remote etc. Taking this example into consideration, your utility will create a SELECT query and pass the user selected parameters and generate a SQL query of the form
SELECT jobs WHERE is_remote=<user_param> AND tech=<user_param> AND compensation=<user_param>
This is just an overview of what needs to be done. There might be some changes based on your exact use case. But this will just about cover what you need to achieve.
Is it possible to nest an Update query within a Select query?
Or is there another method of updating rows given that the first word in the query must be 'Select' and ensuring that it is one query (ie. there is no semicolon between queries).
Furthermore, it must be accepted by JdbcTemplate query (which is why there must be no semicolons).
Thanks for the help.
=====
Update:
I'm trying to inject SQL to update table rows. The table structure doesn't matter but the table must already exist and the rows that it is trying to update must already exist as well.
Example query:
UPDATE users
SET username = 'Mike';
The only requirement is that the first word in the SQL query must be SELECT because that is the only basic sanity check that the server does. Is it possible to do something like this?
=====
Update:
One solution mentioned was to use PL/SQL to call an function that would mutate data. I'm currently looking to see if you can create this function and call it in one or more SELECT statement.
In my java code, I have multiple select statements running against a database. Each select returns a different resultset. Now I want to have all the result sets under one resultset & pass as the return object.
I want the result sets to be children of the final result being passed. How can I achieve this. I am new to Java. Please help.
In addition to the result sets from select, there are also some values in local variable that need to be passed back.
This sounds like joining for me. If you do not modify data then you can use a join in your query and process each row after the values are returned.
If for some reason a join is not an option, then you can create a tree and add nodes at their appropriate position to achieve the hierarchy you want.
I cannot give you more information, as the only specified technology was Java and I do not know what database server are you using.
I'm trying to use SQLQuery object with multiple sql commands. I need to split the query in order to get better performance.
CREATE TABLE x (
id integer,
key integer)
select *
from x, users,.......
where .......
DROP TABLE x
If your issue is creating and dropping tables, create a TEMPORARY table with drop on commit. Then when you commit your db transaction the table will be gone.
The issue is that usually you only get the last statement's results returned. If you need something else, look at wrapping with a user defined function and presenting a single tabular result set back.
i am migrating from ms access database to mysql database with java frontend so that my application can be used in linux as well .
in ms access here's what i would do
go to create query . write a select statement . call give the name of the query as query1.
when double clicking on query1 you would get the result of select statement in a tabular form.
next i would write a query2 which is also a select query. this query would be fetching data not from a table but query1 e.g select a,b from query1;
now i am on a mysql database using java
what would be the java statement for select a,b from query1 ?
what i mean to say is that i would connect to mysql using jdbc.
have query1 like this
string query1 = " select * from users " ;
then execute query using executeQuery(query1)
but i dont think i can do something like this.
string query2 = " select a,b from query1 " ;
and then executeQuery(query2)
so what is the way out ?
I ran into the exact same problems when I went from using MS Access to using a lot of SQL queries against a MySQL DB.
There are two ways I would approach this:
VIEWS:
Views are a great way to emulate a lot of the functionality you found in Access. One of the things I really liked about Access was the ability to separate my SQL into smaller queries and then re-use those queries in other queries. Views allow you to do essentially the same thing in that you define a query in a View and then you can write another query or View against that original View.
In my experience, however, Views tend to be really slow, especially when referencing calculated columns. With MySQL, I very rarely use Views (though perhaps others have found for efficient ways of implementing them).
SUBQUERIES (NESTED QUERIES)
As others have mentioned, subqueries are a great way to write multiple queries within one query. With a subquery, instead of putting the query name (as in Access) or View name (as explained above) within the SELECT portion of your code, you simply paste the entire SQL statement of the subquery.
You might write code like this to find only the 2009 sales and salesperson name for customers in a database:
SELECT
customer.Name,
customer.AccountNumber,
customer.SalespersonName,
ch.`2009 Sales`
FROM
customer
Left Join (
SELECT
customerhistory.AccountNumber,
SUM ( CASE WHEN customerhistory.`Year` = 2009
THEN customerhistory.`Sales`
ELSE 0
END
) AS `2009 Sales`
FROM
customerhistory
GROUP BY
customerhistory.AccountNumber
) ch ON customer.AccountNumber = ch.AccountNumber
In my work, I tend to use mostly subqueries since I find they run a lot faster than views, but your experience may vary.
You can do this all in MySQL. The query would look like
SELECT * FROM (
SELECT * FROM users
) query1;
You can either do nested queries (subqueries) like #muffinista suggested.
But i think you are looking for Views: http://dev.mysql.com/doc/refman/5.0/en/create-view.html.
In short, a view is a "pseudo table" that is a result of a query.
you can
create view q1 as
select * from table1 where f1>1
select * from q1 where f2<100
select * from table2 where user_id in (select user_id from users)