1

I'm using php/mysql and jquery ui to auto-populate. What I need help with is querying two fields from the db (city, state) from a single input (chicago, il). What is throwing me is how to combine the fields in the query.

Example that works

SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = $foo // 'chicago'
OR Zip = $foo // '01234'
GROUP BY City, State
ORDER BY State;

What I'd like to do, but not working

SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = $foo // 'chicago'
OR
City+', '+State = $foo // 'chicago, il'
OR Zip = $foo // '01234'
GROUP BY City, State
ORDER BY State;

(BTW - The state field is an abbreviation.)

The reasoning is that some city names are only 3 or 4 letters and are contained within other city names so the user may need to type a comma and the state to see the auto-suggestion pop-up.

5 Answers 5

1

I tried CONCAT_WS on my db and it worked out fine, the following query should work for you.

Syntax for CONCAT_WS is here: first argument is the separator used to concatenate the strings, which are the other arguments

SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = $foo // 'chicago'
OR
CONCAT_WS(', ',City, State) = $foo // 'chicago, il'
OR Zip = $foo // '01234'
GROUP BY City, State
ORDER BY State;
Sign up to request clarification or add additional context in comments.

2 Comments

Keep in mind that performing a function on the fields will slow down the query as the function will have to be performed on every row in the table. I think a better answer would be to split $foo by ',' and then trim the whitespace. The first term is now the City and the second term, if any, is the state.
@Evil I'll keep that in mind and use php split if I need to break them up. list($city, $state) = split(', ', $_GET['term']);
0

Split your input string before running your query so that you can do:

SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = 'chicago'
OR Zip = '01234'
GROUP BY City, State
ORDER BY State;

The state becomes irrelevant if they've also specified chicago.

2 Comments

the city state and zip are the same variable field being passed in
they are coming in on keystrokes not all at once. so 'chica' would bring up multiple cities, which is fine, then as the user types more 'chicago' it would auto-suggest the different cities in different states named 'chicago' (yes there are some), then as the user defines even more 'chicago, i' it will only auto-suggest chicago, il
0

you can try


SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = 'chicago'
OR( City LIKE "chicago,%" AND State LIKE '%illinois')

OR Zip = '01234'
GROUP BY City, State
ORDER BY State;

2 Comments

but i can't separate chicago and illinois. they come in together, combined in the same field (i.e. chicago, illinois)
Well, i think this is not the best way for going on. if you want not to do your search for state names shorter than 4 char you can actually give up giving autocompletion (actually, if it's 3-4 char, the user should not be helped by autocomplete that it's usefull with long words).
0

I would suggest wrapping your string concatenation in parenthesis. You could also use a function like CONCAT() or CONCAT_WS() in MySQL.

And what do you mean by "it doesn't work" ? The clause breaks or ... ?

Comments

0
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM
   (SELECT City, State, Zip, (City +', ' + State) AS CityState
    FROM Zips) as SQ
WHERE City = $foo // 'chicago'
OR
CityState = $foo // 'chicago, il'
OR Zip = $foo // '01234'
GROUP BY City, State
ORDER BY State;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.