I was actually doing somthing else and came accross this intresing W3schools tutorial/ section in relation to SQL databases (its a curiousity killed the cat thing more then anything)
The page link where my question comes from is as follows
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where
the example lists all records that include mexico in the countrys catagorie
I can for instance change this to spain and all entrys with spain are filterd through.
The question I have is, it seems very unlikely to me that an end user is going to type out the entire code
SELECT * FROM Customers
WHERE Country='Mexico';
everytime they want too search a city (this example thus being mexico). I'm presuming we can get the city name inserted from a textbox, but ive looked can cant fand any examples on how to do this?
Try this:
SELECT * FROM Customers where <Column> like '%MEXICO%';
'%' is the wildcard. So you will find:
" MEXICO"
"MEXICO City"
"MX MEXICO"
If you want to use the wildcard you must use 'like' instead of '='
Related
I have an interesting data, Country names side by side, I need to get each one of them for spesific id.
I just don't know how can I parse those country names.
When I try to locate that mark,
select locate('ΒΆ',facility_country) from table_name,
it only returns me 0, doesn't work. I need to find a way to parse country names from that string.
for each id, I want to multiply my data on countries. Or maybe make a new dim table out of them: id and countries. In order to type my parse code or function, still not sure how to do it though but I can manage, I just need to locate that mark so I can separate.
I tried using Ascii, such like:
select CHAR(182);
This returns me the same mark.
select locate(CHAR(182),facility_country) from table_name;
When I try like that still I can't locate the mark, it only returns me 0.
How can I parse those county names with that Enter Mark? I have done similar things with "," or " " but first time I see something like that.
Edit: When I copy full text it looks like this after I paste:
USA
Australia
Brazil
Canada
France
Germany
Ireland
Israel
Italy
Netherlands
New Zealand
Switzerland
United Kingdom
(stackoverflow puts them side by side like that, on dbeaver this is what I see: )
edit2: #RiggsFolly requested this:
SELECT facility_country from clinical_trials LIMIT 5
output:
There are many lines like the line3.
edit3: #Tangentially Perpendicular solved it. We are looking at rendered image so we don't know what is the raw data, Apperantly its Char(10) and I can locate with it.
I have a database with user details in one table and linked contact details in another table (where the contact details are stored as "content").
I am trying to make a quick search function where you can search for the name or any contact details. So you can either search for name or an email or phone (whatever is in the contact detail field).
This is what I have so far:
SELECT DISTINCT leads.id, CONCAT(first_name,' ', last_name) AS name
FROM `leads`
INNER JOIN `contact_details` ON contact_details`.`lead_id` = `leads`.`id`
WHERE ((CONCAT(first_name, last_name, content) LIKE ('%[XXX]%')));
This works fine. You can search for f ex "55" and it will return hits on f ex ph number 555-573-3222 or you can search for a name string like 'Joh' and it will match 'Johnson'.
My problem, though, is that regardless of what you are searching for, what is being returned is client name. Since this is for an autocomplete feature, this is obviously very confusing. If you start typing in 555-2 you want to see the suggestion 555-221-6362 not "John Johnson".
How can I return EITHER a phone number or email (from column "content") OR the concact first_name, ' ', last_name depending on whether the search matched a name or a contact_detail.content.
Since I am searching on first_name OR last_name, the search works well for "joh" matching "John" but obviously breaks when you search for "John Stan" for "John Stanley". Is there a Mysql way of fixing this or do I need to clean up string before and do alternative searches if there is a space (searching first_name AND last_name separately)
Any suggestions would be greatly appreciated as I have struggled with this for days now.
Charliez, per our comment conversation, the following is an example of how to do some nested if/thens as well as the break out of the where. You'll need to adjust this to met your specific needs, but should give you enough of an example that you should be able to get things working.
SELECT
IF(last_name LIKE '%JAM%',
last_name,
IF(first_name LIKE '%JAM%',
first_name,
''
)
) AS MatchedFieldText
FROM employee
WHERE
last_name LIKE '%JAM%'
OR first_name LIKE '%JAM%';
1) I don't think there is a way to have a SELECT statement return something different based on an OR. My first thought to solve this would be to return first, last, content and use some regex to determine if you should show the name or the number/content.
2) Kind of a hard problem, and I don't think I have seen a perfect solution to it. This might give you some ideas/put you on the right track.
So I have this website where people can report some tracks. People can do so even tho they're not member of said website and if they're not members, the system will assign them a random "pseudo member number" (pmn from here on) in the style of "not_a_member_XXXX".
$query_claimed = "SELECT * FROM claims_archive WHERE t20pctID=:t20pctID AND member_name=:member_name AND member_email=:member_email AND (member_number=:member_number OR member_number LIKE '%not_a_member_%')";
$stmt = $con->prepare($query_claimed);
$stmt->bindParam(':t20pctID', $t20pctID);
$stmt->bindParam(':member_name', $member_name);
$stmt->bindParam(':member_number', $member_number);
$stmt->bindParam(':member_email', $member_email);
$stmt->execute();
In the testing period we have had some songs have been reported by the same person with a pseudo-number and some with a member number. My problem is that if I make a query with a pmn and the song exists with the same member name and e-mail but with another member number, the code will insert it instead of displaying a message in the style of "You have already claimed this song".
But if I do the query with the member number, then it will display the above message even if the record has it reported with a pmn.
I thought there was something wrong with my logic, but running the above query on phpMyAdmin, it does show the record if there is any matches. I have read about precedence, in case it applies here, and in case there's some operator order I should know about. What am I doing wrong?
Thanks in advance and I hope I made myself understood (English isn't my first or second language).
EDIT to add some info: although I haven't been able to identify a specific pattern for this issue, I have identified that it happens with this particular record.
I have a nested repeated structure, the repeated structure is of variable length. For example, it could be a person object with a repeated structure that holds cities the person has lived in. I'd like to find the last item in that list say to find current city person lives in. Is there an easy way to do this, I tried looking around jsonpath functions but I'm not sure how to use it with "within". Any help please?
1) You can use LAST and WITHIN
SELECT
FIRST(cell.value) within record ,
LAST(cell.value) within record
FROM [publicdata:samples.trigrams]
where ngram = "! ! That"
2) or if you want something more advanced you can use POSITION
POSITION(field) - Returns the one-based, sequential position of field within a set of repeated fields.
You can check the samples from trigrams (click on Details to see the unflatten schema)
https://bigquery.cloud.google.com/table/publicdata:samples.trigrams?pli=1
And when you run POSITION, you get the ordering of that field.
SELECT
ngram,
cell.value,
position(cell.volume_count) as pos,
FROM [publicdata:samples.trigrams]
where ngram = "! ! That"
Now that you have the position, you can query for last one.
In my MySQL DB I have a list of terms like this (With First letters Capital and most of the time plurals)
Hairdressers
Restaurants
Beauty Salons
Furnitures For Restaurants
On my website I have a search bar where the user can search for those word (my website is a kind of POI finder). The search bar has a auto-suggest function, but I'm having problems with my query.
SELECT * FROM TABLE WHERE word = 'userword%'
So if the user enter "Res" it will return all the word starting the "Res". Fair enough, that works, But in my DB i have also words with space between them (Furnitures For Restaurants) and I would like that if the user enter "Res" the mysql query ALSO return Furnitures For Restaurants. I have tried this
SELECT * FROM TABLE WHERE word = '%userword%'
Yes great this works, but... it is a bit to much relax.. it return all and everything, so if the user enters "a" it will return all the word having a in it... And I don't want this.
So how can make the query works only on the FIRST letters of each word ? Exactly like my first query but taking in consideration words with spaces?
Look into mysql FULL Text Search feature. Look at this article too, explains mysql full text searching in a very detailed way.