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
Related
I think the solution to this problem should be easy, but I can't find a good way around it.
I'm working with UK postcodes. My reference db looks like this:
+----+----------+----------+
| ID | postcode | leadtime |
+----+----------+----------+
| 1 | AB10 | 1 |
| 2 | AB11 | 2 |
| 3 | B7 | 3 |
| 4 | SE16WD | 1 |
+----+----------+----------+
The value in this table only represents the first half of a full UK postcode, but that's all we need to check against.
Now, the user has a form where they can type their FULL postcode to check what the lead time will be (the full postcode is then use as part of the delivery address).
Uk postcodes can have different length and you can type them with or without spaces between the first and the second half. Right now I have some PHP code that would basically take whatever was typed by the user and try to run it through all the different layout possibility, but it doesn't always work.
I'd like to find a way to do so in MySQL. I've started playing around with
SELECT leadtime FROM myTable WHERE postcode LIKE '$postcode%'
But this only works for the first few digits. As soon as the user types the entire postcode, this query won't find anything in the table.
I cannot for the life of me, find a way to match the two values!!
I'm sure I'm missing something really basic here!
Thanks a lot for your time and help with this.
EDITED the MySQL table to better reflect the variety in reference postcodes to check against.
SELECT * FROM user;
+---------+--------+
| user_id | user |
+---------+--------+
| 3 | George |
| 1 | John |
| 2 | Paul |
| 4 | Ringo |
+---------+--------+
SELECT * FROM user WHERE 'Ringo Starr' LIKE CONCAT(user,'%');
+---------+-------+
| user_id | user |
+---------+-------+
| 4 | Ringo |
+---------+-------+
I see no other way around than stripping the postcode from whitespaces and comparing your DB 'postcode' field with a 4 characters substring of that postcode.
You can always:
make a "broader" selection by using substr($postcode, 0,2);
then loop through the results and trim off the alphabetic characters from the beginning of each result using regex;
then parse the numeric string to number using intval();
and finally make a number-range comparison to narrow down the result.
The above technique may be a bit more complex, but you will have more control over the final results.
If you need a code example of how to achieve this, kindly comment and I will update the answer.
try like this
replace space by wildcard
select USERNAME FROM LOGIN_USERS WHERE USERNAME LIKE CONCAT('%',REPLACE('user_inPut',' ','%'),'%');
I am using codeigniter for my project and implementing search. I want to filter users from users table with cars they own. Structure of this table is shown below
+-----+----------+---------------------+---------------+
| #id | username | cars | other_details |
+-----+----------+---------------------+---------------+
| 1 | MAC | Jaguar,Porche | -- |
| 2 | DEV | Porche,Ferrari,Ford | -- |
| 3 | MONICA | Ford,Audi | -- |
+-----+----------+---------------------+---------------+
On front end, I am selecting cars from checkboxes which are returning car array for find users who have these cars like ["Porche","Ferrari"]. I am not able to find solution for how to get users using codeigniter active records in model. Please help me find out how to get users/rows having cars available in array.
Thanks in advance.
Finally, I find answer.First I had implode array with comma(,) and then use sql query and regular expression for getting an answer like this;
$this->db->sql("select * from users where `cars` NOT REGEXP '$cars'");
This query give data according to my requirement.
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;
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'
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