Mysql PHP , How to select data that start with a specific carachter - php

I have a table cities:
+----+--------+
| id | zip |
+----+--------+
| 1 | 07500 |
| 2 | 07501 |
| 3 | 75000 |
| 4 | 75001 |
| 5 | 75002 |
+----+--------+
And I need to select data where the field zip start with 750, so I've tried :
SELECT FROM cities WHERE ZIP LIKE '%750%'
But this returns me all data in the table that contain 750 and this isn't normal. How could I tell my query to select data that start with 750 ?

Do this query:
SELECT FROM cities WHERE ZIP LIKE '750%'
Explanation
% is a special MySQL character that means any character, empty string or group of characters.
Examples:
LIKE '%a%' -- matches: a, ant, track | any string that contains a in any location
LIKE 'a%' -- matches: a, ant | strings that start with a
LIKE '%a' -- matches: a, data | strings that end with a

This should make the magic:
SELECT FROM cities WHERE ZIP LIKE '750%'
% at begin means any thing before.

LIKE '750%'
should do the trick
if your string contains '%' character than you would need to use \%

Related

Get Matching MySQL Rows from List?

I've got a list of ID numbers, (ex: 100230, 123890, 342098...). I've also got a table, with one column devoted to ID numbers:
thisID | name | dateBirth | State
----------------------------------
192465 | Fred | 94-12-06 | OR
197586 | Alex | 78-04-26 | NM
197586 | Alex | 78-04-26 | CA
178546 | Sam | 65-12-01 | NY
112354 | Katy | 89-06-22 | CO
...
I need to return any rows with 'thisID' that matches any of the items in the list I've got. Also, note that sometimes there may be multiple rows with the same ID that match an item in the list... in that case, all matching records should be returned.
I've looked around, and seen some recommendations to use arrays or temporary tables or something, but nothing definitive. How should I do this?
You can use the IN sql syntax for this, if I understand you correctly.
SELECT * FROM tablename
WHERE thisID IN (100230, 123890, 342098);
You can do like this:
select * from [table_name] where thisID in ([your IDs]);
It will return all the rows that match the given IDs.
See the SQLFiddle Demo

mysql show offset with query

I have some data in my database
---------
| Name | Offset(only for imagine it)
---------
| Aa | 1
| Ab | 2
| Ba | 3
| Cf | 4
| As | 5
---------
when use this query
SELECT * FROM table LIMIT 1,3;
it must be show 3 of the first data, Aa, Ab, Ba, with offset like that.
Now, I want to selecting all of Name with prefix a, so use this query
SELECT * FROM table WHERE Name LIKE 'a%' LIMIT 1,3;
I want to show the result of that query with the offset, how to do this?
Loop it is not the solution I needed.
---------
| Name | Offset(only for imagine it)
---------
| Aa | 1
| Ab | 2
| As | 5
---------
Any help appreciated
Thank you!
You have it mostly correct. However, rows start at 0. So the answer: SELECT * FROM table WHERE Name LIKE 'a%' LIMIT 0,3
That SQL query will select everything in the table table that starts with the letter 'a' (or 'A').
For alphabetical order, use ORDER BY Name.

How to match the comma separated values in mysql and get the values

My table User
+----+-----------+---------+--------+---------+------+-------+
| id | name | email | number | call_no | chek | Extra |
+----+-----------+-------+----------+---------+------+-------+
| 1 | one | a#a.com | 123 | 1164 | 1 | 1,2 |
+----+-----------+---------+---------+---------+------+------+
| 2 | two | a#a.com | 123 | 1164 | 1 | 2,1 |
+----+-----------+---------+---------+---------+------+------+
I have the field called Extra it contains the value 1,2
what is my question is ?
I need to match the 1,2 in the table id and myresult like one,two from the user table
For simple example query.what im tried is in below ?
select name from user Extra in('1,2');
My expected output is
one,two
If i tried to do in explode and if i run it in loop i can get the result but i need this in sql is it possible to get it?
You should have a separate table with one row per id and each value of extra. This is called a junction table. SQL has a great data structure for storing lists of things, called a table not a string. And, storing numbers as characters is even worse.
You can, however, do what you want with a join and aggregation:
select id, group_concat(e.name) as names
from table t left join
table e
on find_in_set(t.id, e.extra) > 0
group by t.id;

MySQL returning results from one table based on id data in another table

I tried to make this inside this question, but i am too young on #stackoverflow to post comments.
MySQL returning results from one table based on data in another table
I cannot get this to work. My intentions are slightly different.
I have two tables (and more in the future) that I intend to work together. I want to keep my db size down, so instead of using full words to reference time_code_department, I added a column to reference the "department_id". now I want to grab all the "time_codes" from table where the "time_code_depart" id matches the variable entered.
So if user selects "Solar" department and time_code_department table has "9" as the "solar" "department_id", then i want to return all the entries in "time_codes" that have the "department_id" "9" on the time_codes table. Which in this example would be lines with id 40 and 75.
Table Structure:
----------------------------------------------
| time_codes (table) |
| |
| id | department_id | code_number | code_name |
----------------------------------------------
| 40 | 9 | 35 | Safety |
| 52 | 10 | 725 | Inventory |
| 75 | 9 | 18 | Cabinets |
----------------------------------------------
-----------------------------------
| time_code_depart (table) |
| |
| department_id | name | manager |
-----------------------------------
| 9 | Solar | John |
| 10 | Finance | Mary |
| 11 | Design | Sue |
-----------------------------------
I've tried to query:
SELECT 'department_id'
FROM `time_codes`
INNER JOIN `time_code_depart`
ON 'time_codes.department_id' = 'time_code_depart.department_id'
WHERE 'name' LIKE 'Solar'
and
SELECT 'time_codes.id', 'time_codes.code_number', 'time_codes.code_name'
FROM `time_codes`
ON 'time_codes.department_id' = 'time_code_depart.department_id'
WHERE 'time_code_depart.name'
LIKE 'Solar'
Both of these I formed based on several readings on the subject, and i have used several variation of sentax. I cannot get it to return the entries for the lines with id 40 and 75.
Can you help me identify where I am going wrong?
You have several problems with quoting.
First, to quote table or column names in MySQL, you use backticks; single quotes are used for making strings.
Second, when you have a table.column, you must quote them each separately.
Note that it normally isn't necessary to quote table and column names at all. They only need to be quoted if they're the same as reserved words, or contain punctuation characters.
SELECT `time_codes`.`department_id`
FROM `time_codes`
INNER JOIN `time_code_depart`
ON `time_codes`.`department_id` = `time_codes_depart`.`department_id`
WHERE `name` LIKE 'Solar'
And when you have long table names like this, I recommend making use of table aliases to make expressions more readable:
SELECT tc.department_id
FROM time_codes AS tc
INNER JOIN time_code_depart AS tcd
ON tc.department_id = tcd.department_id
WHERE name LIKE 'Solar'

mysql find best matching word in one row

i have following table:
------------------------
| uid | attrvalue |
------------------------
| 1 | Spray |
| 2 | strong |
| 3 | very strong |
| 999 | Creme |
------------------------
now i have a query in php and like to find all rows where all queries are found.
SELECT * FROM attrtable WHERE MATCH (attrvalue) AGAINST ('Spray+very+strong' IN BOOLEAN MODE);
I like that it only finds row 1 and 3. But the resultrows are 1, 2 and 3.
So its important that it founds all words or word combinations in the table rows. The query doesn't contain words or combined words which aren't in the table (I check this first).
Just use LIKE, but the other way around to what your probably used to.
select query
from table1
where 'attrvalue' like concat(query,'%')
order by length(query) desc
limit 1

Categories