SQL to filter out some data? - mysql

I have a database table: mydata
id content name
1 1 a
2 2 b
3 2 c
4 13 hhh
5 13 yyyy
6 7 wwww
7 13 iiii
8 7 nnnn
9 8 oooo
It will look at "content" field. If the same value inside "content" show up more than one time, it will display out. Here is final result
id content name
2 2 b
3 2 c
6 7 wwww
8 7 nnnn
4 13 hhh
5 13 yyyy
7 13 iiii
Thus how to write this SQL?

select *
from myData
where content in (
select content
from myData
group by content
having count(*) > 1)

Related

Fast way to combine multiple rows in mysql

I've tried an sql-query like this:
SELECT A.id, FROM_UNIXTIME(A.timestamp) AS timestamp, A.test, B.test2, B.test3, C.test4
FROM table AS A
INNER JOIN table AS B
ON A.id = B.id - 3
INNER JOIN table AS C
ON A.id = C.id - 6
ORDER BY id DESC
To merge different rows from one table together.
But it is very slow. Is there an way to speed it up in MySQL itself. I want to create an view to select the data from tableau (tableau itself has no preprocessing).
With an postprocessing script it would be very easy (just save up to six old columns and read it back six rows later). Shouldn't such an easy task possible with SQL too?
Example:
Original Table
id
timestamp
test
test2
test3
test4
1
…
1
1
1
1
2
…
2
2
2
2
3
…
3
3
3
3
4
…
4
4
4
4
5
…
5
5
5
5
6
…
6
6
6
6
7
…
7
7
7
7
8
…
8
8
8
8
9
…
9
9
9
9
10
…
10
10
10
10
11
…
11
11
11
11
12
…
12
12
12
12
13
…
13
13
13
13
Needed result:
id
timestamp
test
test2
test3
test4
1
…
1
null
null
null
2
…
2
null
null
null
3
…
3
null
null
null
4
…
4
1
1
null
5
…
5
2
2
null
6
…
6
3
3
null
7
…
7
4
4
null
8
…
8
5
5
1
9
…
9
6
6
2
10
…
10
7
7
3
11
…
11
8
8
4
12
…
12
9
9
5
13
…
13
10
10
6
ID is an unique autoincrement value.

SQL: Split Dataset into two columns

I have a table like this:
id c_id time value
1 4 1 12
2 4 2 5
3 4 3 6
4 4 4 48
5 4 5 1
6 4 6 121
7 5 1 121
8 5 2 321
9 5 3 2
10 5 4 1
11 5 5 54
12 5 6 4546
13 5 7 78
14 5 8 784
15 5 9 1
Now I want a table like this with a SELECT command:
time1 value1 time2 value2
1 12 1 121
2 5 2 321
3 6 3 2
4 48 4 1
5 1 5 54
6 121 6 4546
0 0 7 78
0 0 8 784
0 0 9 1
time1 and value1 is from the data with c_id=4,
time2 and value2 is from the data with c_id=5
Is it possible to create a SELECT command to do that?
I hope you can help
Yiu can use an inner join
select a.time as time1, a.value as value1, b.time as time2, b.value as value2
from my_table as a
inner join my_table as b on a.time = b.time
and a.c_id= 4
and b.c_id= 5;

mysql- 3 random records for main departments with multiple level depth of subdepartments

I am trying make a complex query in MySQL i have following two tables
departments employees
Id parent title id name department status
--------------------------- ------------------------------------
1 0 Health 1 abc 3 1
2 0 Sports 2 def 3 1
3 0 Education 3 ghi 5 1
4 1 Physical 4 jkl 10 1
5 1 Mental 5 kkk 6 1
6 2 Football 6 lll 6 1
7 2 Baseball 7 sss 8 1
8 2 Beachball 8 xxx 6 1
9 2 Hockey 9 yyy 6 1
10 4 ENT 10 zzz 7 1
11 0 Finance 11 mnb 11 1
Departments table have four main departments(i-e: parent = 0) with multiple level depths of sub-departments.
Currently i have done it through a PHP function by running queries multiple time to achieve this, but still i want know if this is possible to fetch it with a Query.
Whats the best way OR how to select max 3 employee randomly for each main department with status 1.
The expected result should be something like
id name department maindep
1 abc 3 3
2 def 3 3
3 ghi 5 1
4 jkl 10 1
5 kkk 6 2
7 sss 8 2
10 zzz 7 2
11 mnb 11 11

sql display in matrix form but column and row values not fixed

tables i am using
product_parameter
id productid parameterid value
1 1 1 10
2 2 2 11
3 1 2 34
4 2 4 44
5 3 2 55
6 3 3 43
7 4 1 33
8 1 3 33
9 1 4 24
and so on i want to display in form
parameterid 1 2 3 4. . .
productid
1 43 34 33 24
2 null 11 null 44
3
4
.
.
and so on rows and columns are not fixed
tables i am using
product_parameter
Sorry wanted to correct the output format i needed after query
id productid parameterid value
1 1 1 10
2 2 2 11
3 1 2 34
4 2 4 44
5 3 2 55
6 3 3 43
7 4 1 33
8 1 3 33
9 1 4 24
and so on i want to display in form
parameterid 1 2 3 4. . .
productid
1 43 34 33 24
2 null 11 null 44
3
4
.
.
and so on... rows and columns values are not fixed
It would be much easier to create 2D array in application code.
In SQL you have to add a join for each column.

Selecting items from one column based on values in another.

I have the following data:
id1,id2
1 3
1 8
1 10
1 11
2 3
2 10
2 11
3 2
3 18
3 20
4 3
4 8
5 3
5 10
5 11
5 40
5 45
5 50
6 1
6 59
6 70
I won't get all id1 with id2 = 3,10,11.
For example, id1=4 only with id2=3, should not return.
The results should be
id1
1
2
5
SELECT distinct(ID1) FROM TBTEST WHERE ID2 IN(3,10,11)
SQL code
SELECT ID1,COUNT(ID2) FROM TBTEST
WHERE ID2 IN(3,10,11)
GROUP BY ID1
HAVING COUNT(ID2)=3
Is this what you need?