I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+
Related
+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011768 | ABDUL | 5 |
| 2011499 | ABDULLA | 4 |
| 2011198 | ADNAN | 3 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011749 | AMIR | 1 |
| 2011213 | AMOGHA | 5 |
| 2011027 | ANU | 4 |
| 2011046 | ANUDEV D | 3 |
| 2011435 | B S SAHANA | 2 |
| 2011112 | BENAKA | 1 |
+---------+----------------+--------+
How to sort the number like col_no as 1 2 3 4 5 and again repeat as 1 2 3 4 5?
i need output like this
+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011749 | AMIR | 1 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011198 | ADNAN | 3 |
| 2011499 | ABDULLA | 4 |
| 2011768 | ABDUL | 5 |
| 2011112 | BENAKA | 1 |
| 2011435 | B S SAHANA | 2 |
| 2011046 | ANUDEV D | 3 |
| 2011027 | ANU | 4 |
| 2011213 | AMOGHA | 5 |
+---------+----------------+--------+
You can use row_number() partition by col_no:
select t.*
from t
order by row_number() over (partition by col_no order by fn),
col_no;
Here is a db<>fiddle.
I have this file:
1.nothing
2.o,s,f,d
3.f,d
4.o,s
5.s,f,d
6.s
7.nothing
8.s,f,d
9.o,d
10.s,f
And a table:
describe delete_me;
+------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| privileges | set('o','s','f','d') | YES | | NULL | |
+------------+----------------------+------+-----+---------+-------+
When I try to:
LOAD DATA INFILE 'privileges.txt' INTO TABLE delete_me FIELDS TERMINATED BY '.';
I get this:
+------+------------+
| id | privileges |
+------+------------+
| 0 | |
| 2 | f,s,o |
| 3 | f |
| 4 | o |
| 5 | f,s |
| 6 | |
| 7 | |
| 8 | f,s |
| 9 | o |
| 10 | s |
| 11 | o |
| 12 | |
| 13 | o |
| 14 | o |
| 15 | s |
| 16 | |
| 17 | o |
| 18 | o |
| 19 | s,o |
| 20 | f |
+------+------------+
The letter d just disappears. Why?
I have a test table
mysql> select * from test1 limit 20;
+------+------+----------------------------------+
| id | num | pass |
+------+------+----------------------------------+
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 2 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 3 | 3 | eccbc87e4b5ce2fe28308fd9f2a7baf3 |
| 4 | 6 | a87ff679a2f3e71d9181a67b7542122c |
| 5 | 7 | e4da3b7fbbce2345d7772b0674a318d5 |
| 6 | 6 | 1679091c5a880faf6fb5e6087eb1b2dc |
| 7 | 8 | 8f14e45fceea167a5a36dedd4bea2543 |
| 8 | 11 | c9f0f895fb98ab9159f51fd0297e236d |
| 9 | 10 | 45c48cce2e2d7fbdea1afc51c7c6ad26 |
| 10 | 12 | d3d9446802a44259755d38e6d163e820 |
| 11 | 12 | 6512bd43d9caa6e02c990b0a82652dca |
| 12 | 18 | c20ad4d76fe97759aa27a0c99bff6710 |
...
I want to delete the 1st,3rd and 9th row.So I use such command
delete from test1 limit 1,3,9;
But it doesn't work,is there any workaround can do this?
I have a question very similar to this one, but different (aka, I couldn't extend the answer to that one to fit my purposes. Due to the second WHERE condition, specifically).
I have a table which tracks the visit number for customers. There are two types of visits:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate |
|----|--------------|-------------|-------------|------------|
| 1 | 1 | 1/1/11 | | |
| 1 | 2 | 1/2/11 | | |
| 1 | | | 1 | 1/3/11 |
| 1 | 3 | 1/4/11 | | |
| 2 | | | 1 | 2/2/12 |
| 2 | 1 | 2/3/12 | | |
| 2 | | | 2 | 2/4/12 |
I need to create a new column which has a sort of 'global visit number' as such:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate | GobalVisit |
|----|--------------|-------------|-------------|------------|------------|
| 1 | 1 | 1/1/11 | | | 1 |
| 1 | 2 | 1/2/11 | | | 2 |
| 1 | | | 1 | 1/3/11 | 3 |
| 1 | 3 | 1/4/11 | | | 4 |
| 2 | | | 1 | 2/2/12 | 1 |
| 2 | 1 | 2/3/12 | | | 2 |
| 2 | | | 2 | 2/4/12 | 3 |
I'm getting mixed up on the WHERE condition with which I can do the self-join. Any advice greatly appreciated.
I have three table
td_product
|--------------|------------------------------------------------------------|
| product_id | product_title | compatible_model | |
|---------------------------------------------------------------------------|
| 1 | prod-1 | 1,4,5,6,8 | |
|---------------------------------------------------------------------------|
| 2 | prod-2 | 1,5,6 | |
|---------------------------------------------------------------------------|
| 3 | prod-3 | 4,6 | |
|---------------------------------------------------------------------------|
the i have
td_model
|--------------|----------------------------------------------------|
| model_id | model_title | brand_id | model_slug |
|-------------------------------------------------------------------|
| 1 | mode-1 | 1 | 1-mode-1 |
|-------------------------------------------------------------------|
| 2 | mode-2 | 2 | 2-mode-2 |
|-------------------------------------------------------------------|
| 3 | mode-3 | 4 | 3-mode-3 |
|-------------------------------------------------------------------|
| 4 | mode-4 | 4 | 4-mode-4 |
|-------------------------------------------------------------------|
| 5 | mode-5 | 2 | 5-mode-5 |
|-------------------------------------------------------------------|
| 6 | mode-6 | 4 | 6-mode-6 |
|-------------------------------------------------------------------|
| 7 | mode-7 | 1 | 7-mode-7 |
|-------------------------------------------------------------------|
| 8 | mode-8 | 2 | 8-mode-8 |
|-------------------------------------------------------------------|
and finally the brand
|--------------|------------------------------------------------------------|
| brand_id | brand_title | |
|---------------------------------------------------------------------------|
| 1 | brnd-1 | |
|---------------------------------------------------------------------------|
| 2 | brnd-2 | |
|---------------------------------------------------------------------------|
| 3 | brnd-3 | |
|---------------------------------------------------------------------------|
| 4 | brnd-3 | |
|---------------------------------------------------------------------------|
| 5 | brnd-3 | |
|---------------------------------------------------------------------------|
| 6 | prod-3 | |
|---------------------------------------------------------------------------|
Now i have a query which runs like this
SELECT * FROM td_product,td_model,td_brand
WHERE
td_product.product_id > 0
AND td_model.model_id IN (td_product.compatible_model)
AND td_model.brand_id = td_brand.brand_id
AND td_model.model_slug = '1-mode-1'
Which displays prod-1 and prod-2
but this query return null value.
SELECT * FROM td_product,td_model,td_brand
WHERE
td_product.product_id > 0
AND td_model.model_id IN (td_product.compatible_model)
AND td_model.brand_id = td_brand.brand_id
AND td_model.model_slug = '5-mode-5'
What am i doing wrong?
Instead of IN use FIND_IN_SET(...); change:
AND td_model.model_id IN (td_product.compatible_model)
to:
AND FIND_IN_SET(td_model.model_id, td_product.compatible_model) > 0