Recursive call in mySQL - mysql

I have some data which has three columns
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| a | 1 | p |
| a | 3 | q |
| 1 | b | r |
| 1 | c | s |
| 3 | x | t |
| b | l | u |
| b | x | v |
| c | t | w |
| c | z | m |
+------+------+------+
Column 1 entries can have more than 1 value.
Now if user searches for 1 in the table, it returns all the values corresponding to 1,
now all entries corresponding to b in column 1 should be repeated and so on.. so if user searches for 1 the output should be
+------+------+
| col1 | col2 |
+------+------+
| 1 | b |
| 1 | c |
| 3 | x |
| b | o |
| b | x |
| c | t |
| c | z |
+------+------+
Can somebody help me with the select query.?

Related

How do i show values from the table

how do i show all values from this table
+------+---------+-------+--------+-----+
| A | B | C | D | E |
+------+---------+-------+--------+-----+
| 2 | K | M | NULL | S |
| 3 | L | C | NULL | N |
| 4 | R | P | NULL | N |
| 5 | P | N | C | N |
| 6 | Y | Q | NULL | S |
| 7 | S | T | NULL | S |
| 8 | C | M | NULL | N |
| 9 | X | C | NULL | S |
| 10 | E | Q | NULL | N |
| 11 | A | C | NULL | S |
| 12 | C | NULL | C | N |
| 13 | F | Q | NULL | N |
| 14 | L | NULL | C | S |
| 15 | S | P | NULL | S |
+------+---------+-------+--------+-----+
to this.
show the first 8 values from column A, B and the 7 will be 5 in column A
SELECT A, B FROM letters LIMIT 8;
+------+--------+
| A | B |
+------+--------+
| 2 | K |
| 3 | L |
| 4 | R |
| 5 | P |
| 6 | Y |
| 5 | S |
| 8 | C |
| 9 | X |
+------+--------+
You'll want to use a case expression or the if() function. My preference is a case expression because it's more portable. Furthermore you'll need to supply an ORDER BY clause since records in a table have no ordering.
SELECT CASE WHEN A=7 THEN 5 ELSE A END as A, B FROM letters ORDER BY letters.A LIMIT 8;

mysql query for adding non existing rows with count zero

"Table A" & "Table B" are my initial tables for the query. Result is the final table as shown in the following figure.Find Non-existing rows
Table A:
| Date | Name | Count |
|:----------:|:----:|:-----:|
| 01-01-2020 | A | 5 |
| 01-01-2020 | B | 15 |
| 02-01-2020 | B | 20 |
| 02-01-2020 | C | 15 |
| 02-01-2020 | D | 30 |
| 03-01-2020 | A | 30 |
| 03-01-2020 | D | 10 |
Table B:
| Name |
|:----:|
| A |
| B |
| C |
| D |
Result:
| Date | Name | Count |
|:----------:|:----:|:-----:|
| 01-01-2020 | A | 5 |
| 01-01-2020 | B | 15 |
| 01-01-2020 | C | 0 |
| 01-01-2020 | D | 0 |
| 02-01-2020 | A | 0 |
| 02-01-2020 | B | 20 |
| 02-01-2020 | C | 15 |
| 02-01-2020 | D | 30 |
| 03-01-2020 | A | 30 |
| 03-01-2020 | B | 0 |
| 03-01-2020 | C | 0 |
| 03-01-2020 | D | 10 |
cross join distinct dates from a to distinct id from b then left join a
drop table if exists t,t1;
create table t
(id varchar(1),dt date);
create table t1
(id varchar(1));
insert into t values
('a','2019-01-01'),('b','2019-01-01'),
('b','2019-02-01'),('d','2019-01-01'),
('c','2019-03-01');
insert into t1 values
('a'),('b'),('c'),('d');
select t3.id,t3.dt,t.id,t.dt
from
(
select distinct dt,s.id from t
cross join
(select distinct id from t1) s
) t3
left join t on t.id = t3.id and t.dt =t3.dt
order by t3.dt,t3.id;
+------+------------+------+------------+
| id | dt | id | dt |
+------+------------+------+------------+
| a | 2019-01-01 | a | 2019-01-01 |
| b | 2019-01-01 | b | 2019-01-01 |
| c | 2019-01-01 | NULL | NULL |
| d | 2019-01-01 | d | 2019-01-01 |
| a | 2019-02-01 | NULL | NULL |
| b | 2019-02-01 | b | 2019-02-01 |
| c | 2019-02-01 | NULL | NULL |
| d | 2019-02-01 | NULL | NULL |
| a | 2019-03-01 | NULL | NULL |
| b | 2019-03-01 | NULL | NULL |
| c | 2019-03-01 | c | 2019-03-01 |
| d | 2019-03-01 | NULL | NULL |
+------+------------+------+------------+
12 rows in set (0.05 sec)
PS - if you want solutions which are close to your model you should include table definitions and sample data as text in the question (which we can use) rather than links to images (which cannot use)

Find all rows with value x if there is a pattern of occurence using MySQL

I have two tables
Table A
--id--something--
| 1 | x |
| 2 | y |
| 3 | x |
| 4 | z |
| 5 | x |
| 6 | z |
Table B
--id-----A.id-----Value--
| 1 | 1 | 0 |
| 2 | 5 | 1 |
| 3 | 10 | 1 |
| 4 | 17 | 1 |
| 5 | 19 | 0 |
| 6 | 34 | 1 |
I want to find all "somethings" from Table A, that have at least one pattern: there is a row that have a relation to Table B with value = 0 and next row with the same value in column "something" doesn't have relation to Table B at all.
In this case it would be:
x
because:
Joined Table:
--id--something-- --id-----A.id-----Value--
| 1 | x | | 1 | 1 | 0 |
| 3 | x | |NULL| NULL | NULL |
As mentioned, while certainly do-able, this is quite a complicated request. Anyway, here's something to get you started...
SELECT x.*
, MIN(y.id) next_id
FROM a x
JOIN a y
ON y.id > x.id
AND y.something = x.something
GROUP
BY x.id;
+----+-----------+---------+
| id | something | next_id |
+----+-----------+---------+
| 1 | x | 3 |
| 3 | x | 5 |
| 4 | z | 6 |
+----+-----------+---------+

How to group by / group_concat each set of results?

Image we have a table like this:
table1
+----------+----------+--------+------------+
| position | epoc | name | value |
+----------+----------+--------+------------+
| 1 | 1 | A | v01 |
| 1 | 1 | B | v02 |
| 1 | 1 | C | v03 |
| 1 | 2 | A | v04 |
| 1 | 2 | B | v05 |
| 1 | 2 | C | v06 |
| 1 | 3 | A | v07 |
| 1 | 3 | B | v08 |
| 1 | 3 | C | v09 |
| 1 | 4 | A | v10 |
| 1 | 4 | B | v11 |
| 1 | 4 | C | v12 |
| 2 | 5 | A | v13 |
| 2 | 5 | B | v14 |
| 2 | 5 | C | v15 |
| 2 | 6 | A | v16 |
| 2 | 6 | B | v17 |
| 2 | 6 | C | v18 |
| 2 | 7 | A | v19 |
| 2 | 7 | B | v20 |
| 2 | 7 | C | v21 |
| 2 | 8 | A | v22 |
| 2 | 8 | B | v23 |
| 2 | 8 | C | v24 |
+----------+----------+--------+------------+
I want to be able to get this table:
table2
+----------+--------------------+
| position | value |
+----------+--------------------+
| 1 | v01,v02,v04,v05 |
| 2 | v13,v14,v16,v17 |
+----------+--------------------+
the conditions are:
JUST the "value" of rows with "name" A OR B;
JUST "epocs" that are the first 2 unique results in "position" (epoc 3,4,7,8 are discarded)
GROUP by table1 position (for each position I want the concat of the values that match previous conditions)
This might be what you are looking for:
select position,
group_concat(value order by overall_row_num) value
from
(
select position,
name,
value,
epoc,
#num := if(#position = `position`, #num + 1, 1) as group_row_number,
#position := `position` as dummy,
overall_row_num
from
(
select position, name,
epoc,
value,
#rn:=#rn+1 overall_row_num
from t1, (SELECT #rn:=0) r
where name in ('A', 'B')
order by position, epoc
) x
order by overall_row_num
) x1
where group_row_number <= 4
group by position
See SQL Fiddle with demo

Is there a way in SQL (MySQL) to do a "round robin" ORDER BY on a particular field?

Is there a way in SQL (MySQL) to do a "round robin" ORDER BY on a particular field?
As an example, I would like to take a table such as this one:
+-------+------+
| group | name |
+-------+------+
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | D |
| 2 | E |
| 2 | F |
| 3 | G |
| 3 | H |
| 3 | I |
+-------+------+
And run a query that produces results in this order:
+-------+------+
| group | name |
+-------+------+
| 1 | A |
| 2 | D |
| 3 | G |
| 1 | B |
| 2 | E |
| 3 | H |
| 1 | C |
| 2 | F |
| 3 | I |
+-------+------+
Note that the table may have many rows, so I can't do the ordering in the application. (I'd obviously have a LIMIT clause as well in the query).
I'd try something like:
SET #counter = 0;
SELECT (#counter:=#counter+1)%3 as rr, grp, name FROM table ORDER by rr, grp
What you can do is create a temporary column in which you create sets to give you something like this:
+-------+------+-----+
| group | name | tmp |
+-------+------+-----+
| 1 | A | 1 |
| 1 | B | 2 |
| 1 | C | 3 |
| 2 | D | 1 |
| 2 | E | 2 |
| 2 | F | 3 |
| 3 | G | 1 |
| 3 | H | 2 |
| 3 | I | 3 |
+-------+------+-----+
To learn how to create the sets, have a look at this question/answer.
Then its a simple
ORDER BY tmp, group, name
You can use MySQL variables to do this.
SELECT grp, name, #row:=#row+1 from table, (SELECT #row:=0) r ORDER BY (#row % 3);
+------+------+--------------+
| grp | name | #row:=#row+1 |
+------+------+--------------+
| 1 | A | 1 |
| 2 | D | 4 |
| 3 | G | 7 |
| 1 | B | 2 |
| 2 | E | 5 |
| 3 | H | 8 |
| 1 | C | 3 |
| 2 | F | 6 |
| 3 | I | 9 |
+------+------+--------------+