Result for first element using In statement - mysql

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

Related

how to sort sql data for given table?

+---------+----------------+--------+
| 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.

Join two tables on nearest matching string

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 |
+----+------------------+----+----------------+

How to delete some specify row from a table

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?

Enumerate rows in mysql based on groups with dates in different columns

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.

How to calculate the total value of two column from 3 different tables?

For example,I have 3 tables;
table1:
+-------+
| count |
+-------+
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
+-------+
table2:
+-------+
| count |
+-------+
| 3 |
| 0 |
| 0 |
| 0 |
| 0 |
+-------+
table3:
+-------+
| count |
+-------+
| 1 |
| 1 |
| 0 |
| 0 |
| 1 |
+-------+
I want to calculate table1.count+table2.count+table3.count, to get the result,table_right:
+-------+
| count |
+-------+
| 5 | (1+3+1=5)
| 1 | (0+0+1=1)
| 0 | (0+0+0=0)
| 0 | (0+0+0=0)
| 4 | (3+0+1=4)
+-------+
However, if I use command :
select table1.count+table2.count+table3.count as total
from table1,table2,table3;
The result will become to:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
+-------+
This is not the result i want, If I try
select distinct table1.count+table2.count+table3.count as total
from table1,table2,table3;
I will get:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 7 |
| 2 |
| 1 |
| 3 |
| 6 |
| 0 |
+-------+
Still isn't the result I want. How could i do to get table_right?
if you add a common id (lets call id rowId and lets assume it has the same name on every table),
SELECT t1.count + t2.count + t3.count AS total
FROM table1 AS t1
LEFT JOIN table2 AS t2 using (rowId)
LEFT JOIN table3 AS t3 using (rowId)
if you not have those ids, all i can think about its summing all t1 then all t2 then all t3 and finally add the results together.
SELECT t1+t2+t3 as total
FROM (SELECT (SELECT SUM(count) from table1) as t1,
(SELECT SUM(count) from table2) as t2,
(SELECT SUM(count) from table3) as t3
)
Check out this SQLFiddle
EDIT (2)
to add rowId just alter the tables:
ALTER TABLE table1 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table2 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table3 ADD COLUMN rowId int not null auto_increment primary key;