I need to write the SpringData method using Like "%".
For example,
#RequestMapping (value = "/ users / {firstname} / {lastname}", method = RequestMethod.GET)
List <User> users = findByFirstnameLikeAndLastnameLike (firstname, lastname);
In this case, if both firstname and lastname are equal to %, then all User must return.
I now return a blank page, it works only if both firstname and lastname have some value
I think that's the wrong SQL.
This is really FOUR queries, not one:
SELECT *
SELECT * WHERE FIRST_NAME LIKE '%X'
SELECT * WHERE LAST_NAME LIKE '%Y'
SELECT * WHERE FIRST_NAME LIKE '%X' AND LAST_NAME LIKE '%Y'
You should not try to cover all four cases in a single query.
I would write a DAO with four distinct methods and have the controller choose which one to call based on the input parameters.
Your contract with users should spell out the REST URL differences between the four cases. It's confusing without a contract. Are first and last name required? I can't know unless you tell me.
Related
I'm implementing a rudimentary form of search for my company backoffice system. I would like to find all products names that contain all words in a search query.
So if I have these two products:
Deodorant with cucumber flavor
Deoderant with apple flavor
the search query: cucumber deoderant should match only Deoderant with cucumber flavor.
I can make it work for the query deoderant cucumber like so:
SELECT product_name FROM products WHERE name ~* regexp_replace('deoderant cucumber', '\s+', '.*', 'g');
but this approach does not work when the order is not the name in the query and the product name.
I know that this is possible by doing something like
SELECT product_name FROM products WHERE name ~* 'deoderant' AND name ~* cucumber';
but I would ideally like to stay away from string interpolation as it becomes a bit messy in my current environment.
Is there another, more elegant way to do this?
You could convert the value into an array and then use the "contains" operator #> with the words you are looking for:
SELECT product_name
FROM products
WHERE regexp_split_to_array(lower(name), '\s+') #> array['deodorant', 'cucumber'];
Online example: https://rextester.com/GNL7374
I am a First time user of StackOverFlow here!
I have been trying to figure this out for two days and have come up short.
We have a form that displays every single Client / Customer that we have at the firm, in a continuous form view.
We want to be able to display on this form the date, for each client, when we last communicated, or called, the client. (We want to be sure that we prevent a situation where we have not called a client for more than 1.5 months).
I have a query on a table tracking our correspondence, and other activities, regarding our clients that, in SQL, looks like:
' Query ContactCommunications
SELECT Context, ID, NoteDate, ContactID
FROM Comments
WHERE (((Context)="Communication with Client" Or (Context)="Phone Call with Client"));
(ContactID is a secondary key for Contacts table - we are tracking not
only clients but also opposing parties and such)
This is intended to show all the dates we called or communicated with our clients.
I have a second query that then gets the Last Date from this table, grouped by ContactID, which looks like:
' Query qryLastCommunicationAgg
SELECT ContactID, Last(CommentDate) AS LastOfCommentDate
FROM Comments INNER JOIN qryContactCommunications
ON Comments.ID = qryContactCommunications.ID
GROUP BY Comments.ContactID;
My question is how do I get the query result (When we last called each client) into a text field in our Continuous form list? At the moment there will be some null values as well.
I have tried the expression:
=DLookUp("CommentDate","qryLastCommunicationAgg",[ID]=[ContactID])
But it does not work, giving me #Name?
Not sure what I did wrong. :-(
I appreciate greatly any assistance or suggestions!
-Glenn
First, use Max:
SELECT ContactID, Max(CommentDate) AS LastOfCommentDate
Then try with:
=DLookUp("LastCommentDate","qryLastCommunicationAgg","[ID]=" & [ContactID] & "")
("Below is the fixed version of the DLookup script - Glenn")
=DLookUp("LastOfCommentDate","qryLastCommunicationAgg","[ContactID]=" & [ID] & "")
I've tried searching on here for my answer but have had no luck. I've tried many things to get this to work but haven't been successful so I thought I would post my question. I may repeat myself several times but I'm trying to be as specific/detailed as I can.
I have a form which has an append query to transfer all fields on the form into a table. On this form, I have an invisible control (textbox) which pulls the users Windows login ID and uses that as its default value. I have another field (textbox) for the user's full name.
I have ran a query to pull employee data from my tables to have the employee ID (same as their Window's login ID, first name, and last name. I have also ran an expression to concatenate the two name fields into one to show their full name.
What I've been trying to do is have it so that when someone opens up the form, their full name is already pre-loaded. As I stated above, this is pre-loaded based on a comparison of their Window's login ID with their employee ID that we have on record as they're the same. It's essentially Excel's version of vlookup() that I'm trying to do. I've tried dlookup() but was unsuccessful.
Does anyone know how to make the default value of the employee name control on the form to be determined by employee ID control which has its default value based on the user's Window login ID?
Example: Let's say that I have an employee called Fred Flintstone. In the database there's a table with fields for his first name and last name. There's also a field for his employee ID which is A111111.
When opening up the form, there's an invisible textbox control which has its default value derived from the Windows login ID that the employee used to sign onto the computer with. This Windows login ID happens to be A111111 which is the same as the employee's employee ID.
A query was created that has fields for the employee ID, First Name, Last Name, and an expression to concatenate the two name fields. So the first row would be: | A111111 | Fred | Flintstone | Fred Flintstone |. I don't know how to make tables on here yet so bear with me.
Going back to the form, there's a field for Employee Name. How would I get it to show Fred Flintstone as the default value upon opening the form?
Note: The employee name field will eventually be locked so that it cannot be changed by the employee. I don't know if that would have any impact but I thought that I would mention it.
In the onload event of your form (really rough code, will require some tweaking..):
dim db as dao.database
Dim rs as dao.recordset
Dim username as string
dim SQL as string
username = Environ("USERNAME") 'There are better ways to get username, but this is just a quick example.
SQL = "SELECT * FROM Your_Query WHERE username = '" & username & "'"
set db = currentdb
set rs = db.openrecordset(SQL,dbopensnapshot)
if rs.recordcount > 0
rs.movefirst
me.UserNameField = rs!username
else
msg "User Not Found"
End If
set rs = nothing
set db = nothing
I select field in database using sql command
partner_obj = self.pool.get('res.partner'). browse (cr, uid, ids, *args)
partner_name = partner_obj.ids
customer = (partner_name)
cr.execute("select b.city from res_partner a, res_partner_address b where b.partner_id = a.id AND a.id = %s",(customer))
ads = cr.fetchone()
city = ads and ads[0] or None
but, I just know we must convert into string. I already try to make like this (the code above), but still doesn't work.
I made like this, because you want to know SO is in a message in which the city
may you help me, please
thank you
I don't think you need to run raw SQL, you should be able to use the browse records to get the information you want. I wasn't sure exactly what you wanted from your question, but I'll assume that you are passing in a list of partner ids, and you want back a list of all the city names that they have addresses in. I have not tested this code, it's just a suggestion.
def find_cities(self, cr, uid, partner_ids, context=None):
cities = set()
partner_obj = self.pool.get('res.partner')
for partner in partner_obj.browse(cr, uid, partner_ids, context):
for address in partner.address:
if address.city:
cities.add(address.city)
return list(cities)
First point in OpenERP city is not relation field so when you will trigger sql to fetch the city you will get the name but if you have any customization which stores the city code then you need one more SQL injection to fine out the matching city from city code.
If like if you have city as relation fields with res_partner_address so in that case city field will store only id of the relation record table so when you use sql injection what you will get is id of the city which is linked to your address.
Let me know if I am missing something, Thank You.
Let's say you have three textboxes that you can use to search for data. Each textbox will correspond to a column on the DB2 table. The search string you enter will be inserted into the where clause. For example, you have First-Name, Last-Name, and Phone Number. If you don't enter data into a particular textbox, I default its value in the where clause to '_', the wildcard character to select everything. Also, lets say Phone Number is defined as NULL on the table.
Cursor1 will be used if the user has entered a Phone number to search for. So the where clause will look something like this:
Where FIRST_NAME like :firstname AND
LAST_NAME like :lastname AND
PHONE_NBR like :number
This works when data is entered for phone number. But if a search is done for First Name only, the cursor returns partial or no results because the :number host variable will be populated with the "_' wildcard. PHONE_NBR like '_' will only return the rows that have a real value. If there is a null for PHONE_NBR on a row that matches the First Name you searched for, that row won't show up. So I created a second cursor.
Cursor2 will be used if the user HAS NOT entered a Phone number to search for. The Where clause looks something like this.
Where FIRST_NAME like :firstname AND
LAST_NAME like :lastname AND
(PHONE_NBR like :number OR
PHONE_NBR IS NULL)
So again, if a search was done for a first name only, and some values in PHONE_NBR have data, some are null, EVERYTHING that matches the first name that is searched for will show in the results - which is good. For the rows with values in PHONE_NBR, PHONE_NBR like '_' will get those. For the rows with null in PHONE_NBR, PHONE_NBR IS NULL will get those.
This is a minor yet necessary difference. Because of this minor difference, I would like to combine these two cursors into one. How can that be done to achieve the same results?
Ian, i think the difference is if the user supplies a number he doesn't want to return rows with a null. using cursor 2 all the time would return rows with null along with matching numbers.
You could try a CASE statement based on :number...though i'm not sure if you can use a CASE with "is null" syntax. i know you could if you were just checking for different values (equal to, less than, etc).
The way I'd recommend handling this is by building the query to supply conditions only on the columns where the user enters data. That is:
If the user enters something in the First_Name text box, you have a condition such as:
FIRST_NAME LIKE '...'
If the user enters something in the Last_Name text box, you have a condition such as:
LAST_NAME LIKE '...'
If the user enters something in the Phone_Nbr text box, you have a condition such as:
PHONE_NBR LIKE '...'
In each case, the 3 dots represent a string derived from the information typed into the text box, and the function that does that conversion is fully aware of quoting (to avoid SQL injection).
If the user types in two or three of the text boxes, the independent conditions are joined by an AND. If the user types nothing, you can generate a tautology such as 1 = 1 as the condition.
You then append that condition to the WHERE clause of the SQL statement, and then arrange to execute it.
This is the technique made available by the CONSTRUCT statement in IBM Informix 4GL; it has been available there since 1986. It allows for conditions other than just LIKE, such as equals, less than, greater than or equal to, ranges, or even a list of alternatives (for an IN ('val1', 'val2', ...) condition), and it can be used for all data types.