I have a list of names in my database and I have to find the id of the name that I pass in the url.
My problem is that the names I pass in the url will not have a space in them while the saved record will in the database. When I search the database I get no records found.
e.g database record is "My Name" while what I will be passing in the url and searching with is "myname"
if(isset($_GET["name"])) $name = $_GET["name"];
SELECT id
FROM table
WHERE name Like '%$name%'
Thanks for any help.
// id don't know the exact syntax, but this is what you are searching for I guess:
// replace spaces with nothin temporarily, then check equal (=) not like (%%) if name is exactly the same (except the spaces)
SELECT id, REPLACE(name, ' ', '') AS tmp FROM table WHERE tmp='%$name%'
Obviously not the right way to store or search for, but if you must, try replacing the spaces with blanks like this:
SELECT id
FROM table
WHERE REPLACE(`name`,' ','') LIKE '%$name%'
I think best practice is to store another column, called something like name_nospaces and insert myname as a calculated value into it when you insert the My Name record. You could also create a view that contains myname as a column and query for it, but the advantage of another column in the original table is that it can be indexed for fast retrieval.
Related
I got table promos, with field store_id_list (VARCHAR). what i want to achieve here is i want this promo can be available into multiple store in 1 record, instead using multiple record, the store_id_list value is the list of store_id separated by comma (ex: 1,4,5,7,)
Now, i want to get record of table promo, where i have single store_id value, ex: store_id=5 how can i do that in MySQL? can i do that in single query? if using LIKE then more likely i can get 15 or 45 instead of 5.
My advice is to normalize your tables, and have a db-record per store/promo. But it can be done like this:
Make sure you have commas at the beginning and end of the column value, like this:
store_id_list : ,1,4,5,7,
And then query like this:
... where store_id_list like '%,5,%'
I think you are looking for FIND_IN_SET function. For example:
SELECT * FROM tbl WHERE FIND_IN_SET('5', store_id) > 0;
I have a column in table which has possible values of tom,john,jerry(SET). I have a variable that a user enter and I store it in $var and want that at some point. I could check that if the value in $var exists in particular row then don't update else update, How I can do this in PHP.
I have several rows like tom,john
john,jerry
I dont want tom,john,tom
I have one suggestion.
Please change the database values so that you have: ,tom,john,tom, (Notice the appending and preceding commas ,.
Now if you want to search for tom, fire an SQL like:
SELECT id FROM DB_TBL WHERE field LIKE '%,$val,%'
Repeat this for all values user has entered OR put in a single text box with commas.
Hope this works.
I have a table on MySQL, called Items with 2 columns: Id and Name. I want to show my items, filtering by its name first character. To do this, now I'm using this query:
Select Id,Name from Items WHERE Name LIKE 'a%'
I have 2 questions:
1) It is this the best method to achieve that?
2) To create the filter view, I want to know which characters have at least one item name starting with it. For example, I don't have any item name starting with "X". How could I know which ones have with a single query?
Thanks
If you have an Index on Name then your query is fine.
To get all characters having Names
Select substr(Name, 1, 1) as starting_character
from Items
group by starting_character
order by starting_character
You could run a large OR query like....
select * from Items where nobre like "A%" or nombre like "B%" or nombre like "C%" //etc etc
But if speed and table size is an issue, I would create another column, and just enter the letter value as an INT into that column. Kinda like an index.
So when you insert any new values, just code up a function to return the letter value of the first letter, and insert that into the new column.
Now you can query the column for any letter, based on that integer.
Hope that came out clearly, Im not very good at explaining things sometimes.....
This will give you all the first characters - only once (hence the DISTINCT)
SELECT DISTINCT SUBSTR(Name,1,1) FROM Items ORDER BY SUBSTR(Name,1,1)
Then you can use this to create your filters.
I'm using flavorPHP framework so I have my user table with the field group... I need to get all data with the group = x value, like this:
$this->findAllBy("group", $x);
But it crashes... Any idea?
at first sight it looks like you're using a mysql reserved word as name for the field, try changing the name or using the back quotes `group` where the query is formed to be executed.
I am trying to get a Select statement to work, I have a table(districts) with Names field and I only want to return a name if it is contained in a text field in another table.
Something like this.
SELECT name FROM districts,users
WHERE users.accesslist LIKE '"%"+districts.name+"%"'
This returns an empty list.
I can get it to work if I use a specific name %Swindon% so is it my concatenation that is wrong?
Looks like it is. Use CONCAT("%", districts.name, "%")
You need to specify some sort of relationship between the tables. As it stands now, you're selecting from two tables, but specifying a condition on only one of them.
SELECT name
FROM districts, users
WHERE users.accesslist LIKE '%{$district_name}%' AND districts.name = users.district
would be an example. You didn't specify your table layout, but there must be a field that's common between the two tables, and those are the ones you specify the join condition on.
As well, your query has a syntax error. The doublequotes within the LIKE clause shouldn't be there.
Without knowing the table schema (primary/foreign keys) a subquery should do it. But it is horrible. If you can give use the table schema we can give you something bettter.
This will return all the names in district if they are in users.
SELECT name
FROM districts AS d
WHERE d.name = SELECT name FROM users AS u
LIKE '%Example%' command return All fields like ...AAExampleBB...
LIKE '%Example' command return All fields like ...AAExample
LIKE 'Example' command return All fields like Example
You can use REGEX '^Example$' too.