I have a routine which returns a list of zip codes within so many miles of a particular zip code. I can call the routine and get the result list:
CALL Location.GetNearByZipCodes(28078,5);
Result Set:
28031,28070,28078,28205
I would like to do a query where it selects all records with the output from the routine as part of the where clause:
select * from Location where zipcode in (Location.GetNearByZipCodes(28078,5));
However, this does not work. Is it possible to use the results from the function as part of the where clause? If so, what is the correct syntax?
The above select query fails with:
Error Code 1305: Location.GetNearByZipCodes does not exist.
Thanks in advance for your help!
Try this:
"select * from Location where zipcode in (".Location.GetNearByZipCodes(28078,5).");"
Also make sure this function returns a string with zipcodes saperated by commas ,.
You can also call Location.GetNearByZipCodes() before the query and store the returned value in a variable and then use the variable in the query the way you're used to do it.
Related
i am working on a table that includes a filter function.
For the filter i use a form where i enter the parameters.
Those are added to a string which is my SQL query.
So far it works fine.
There is oine input field where multiple parameters canbe added.
The plan is to seperate them with ; .
For example 520;521;522
My plan was to use str_replace to convert this in to sql Code.
For example
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
But some how this code does not show the expected results.
I only get results for '%520%'
How do i need to adjust this query in order to have the sql query working?
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
In another input field i search for names.
Here the query looks like this...
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%')
This works fine!
The multiple like should be written as,
SELECT *
FROM MaschinenVorgangslisteMitHV
WHERE VorgangNr REGEXP '520|522|523';
I believe you need to add VorgangNr LIKE after every OR.
In my table I have a field with data such as 1,61,34, and I need to see if a variable is in that.
So far I have this
SELECT id, name FROM siv_forms WHERE LOCATE(TheVariable, siteIds) > 0
Which works, with the exception that if the siteIds were 2,61,53, and TheVariable was 1, it would return the row as there is a 1 in 61. Is there anyway around this using native MySql, or would I need to just loop the results in PHP and filter the siteIds that way?
I've looked through the list of string functions in MySql and can't see anything that would do what I'm after.
Try with find_in_set function.
SELECT id, name FROM siv_forms WHERE find_in_set(TheVariable, siteIds);
Check Manual for find_in_set function.
I have a string returned from a function "'aa','bb','cc',..."(the function uses GROUP_CONCAT). I want to use this as a condition in the IN clase of mysql.
SELECT name,class,comment
FROM vwstudent v
WHERE name IN (select func())
I want the query to act like
SELECT name,class,comment
FROM vwstudent v
WHERE name IN ('aa','bb','cc','dd',..)
I assume ('aa','bb','cc','dd',..) is acting as a whole string here and isn't generating any result. What can I do to run this query error less.
I'm not sure if it'll help. But you might be able to hack something together by using PREPARE and EXECUTE as described here: How To have Dynamic SQL in MySQL Stored Procedure
... but that's completely new to me. I've never used anything like that in MySQL.
Background
Creating a wrapper function for a SQL statement.
Problem
A function returns 1 row, whereas the query upon which the function is based returns 100+ rows. The parameter values are identical:
SELECT * FROM as_rpt.tasc_fsa( 'ABC', '2010-01-01'::date, '2011-01-01'::date );
The tasc_fsa function is a SELECT statement with a few table joins; the function language is 'sql' rather than 'plpgsql'.
Question
What reasons would a function return a single row yet the exact same query that the function uses, when not called via the function, correctly returns over 100 rows?
Any ideas would be most helpful.
Thank you!
Have you use RETURN SETOF ...?
SETOF indicates you want to return more than 1 row.
I'm pretty sure you forgot to use 'RETURN SETOF'.
You mention the function "joins a few tables". Your example obviously doesn't. If there are joins being performed, it is possible this is causing less results.
The way the dates are being interpreted is another possibility. When you get 1 answer, is it within the range you specify?
I'm trying to use the prepareStatement function. The code is below. After it executes, it returns me a bunch of vlicense strings instead of the values.
When the code finishing the statement.setString(), the statement becomes:
select 'vlicense' from Vehicle
However, it needs to be:
select vlicense from Vehicle
without the quotation marks. Can anyone tell me what's the problem?
statement = oConnection.prepareStatement("select ? from Vehicle");
String tempString = "vlicense";
statement.setString(1, tempString);
resultSet = statement.executeQuery();
You can't use parameter markers for column names, table names, data type names, or basically anything that isn't data.
When you add a bind variable to a statement like this it is escaped, so that actual SQL string in your example would go to the database as "SELECT 'vlicense' FROM Vehicle', selecting a literal string instead of the column name you want.
You need to concatenate that variable column name into your SQL statement before you prepare it:
statement = oConnection.prepareStatement("SELECT " + vlicense + " FROM Vehicle");
Bind variables are really for query parameters as opposed to dynamic queries.
The ? can't be used to specify the fields, just to do some filters in your query like:
statement = conn.prepareStatement("select field from Vehicle where name=?");
In your case your query is built as:
select 'vlicense' from Vehicle
which means: GET ME A STRING 'vlicense' FOR EACH RECORD OF 'Vehicle'. And you'll get n repeated strings depending on the number of records in your table
It has nothing to do with jdbc, prepared-statements or mysql.
It's just a wrong sql statement.
If you type:
Select 'justanexample' from Vehicle
and the table contains 4 lines, you will get 4 times
'justanexample'
'justanexample'
'justanexample'
'justanexample'
as result.
You did not specify your the table structure, but I guess the
statement should somehow look like this:
select * from Vehicle where license = ?