I need to find a way to find what conversions are missing.
I have three tables.
Table 1: type, which are the different types.
id
name
1
typeA
2
typeB
3
typeC
Table 2: section, which are the different sections.
id
name
1
section1
2
section2
3
section3
4
section4
Table 3: conversions, which contains all the combinations to go from one type to another for each of the sections.
id
section_id
type_convert_from
type_convert_to
1
1
1
2
2
2
1
2
3
3
1
2
4
4
1
2
5
1
1
3
6
2
1
3
7
3
1
3
8
4
1
3
9
1
2
1
10
2
2
1
11
3
2
1
12
4
2
1
For example some are missing from the table above, how can I identify them with a SQL query? Thanks!
Try this. The cross join of table type with itself generates all possible combinations of type id's. I've excluded combinations in the cross join where id_from = id_to (ie you're not interested in conversions from a type to itself)
select * from conversions C
right join (
select T1.id as id_from, T2.id as id_to
from type T1 cross join type T2
where T1.id <> T2.id
) X on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
where C.type_convert_from is null
If you want to check missing type conversions by section, extend the cross join by adding the section table to include section.id as follows. It will list missing type conversions within each section.
select X.section_id, X.id_from, X.id_to from conversions C
right join (
select S.id as section_id, T1.id as id_from, T2.id as id_to
from types T1 cross join types T2 cross join section S
where T1.id <> T2.id
) X
on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
and C.section_id = X.section_id
where C.type_convert_from is null
Related
T1
SCHOOL STUDENT TEACHER
1 1 A
1 2 B
1 3 B
1 4 B
T2
SCHOOL STUDENT TEACHER
2 7 A
2 6 A
2 8 B
2 9 B
T3
SCHOOL TEACHER ID
1 A FOX
1 B CAT
2 A DOG
2 B MOUSE
T4
SCHOOL STUDENT TEACHER ID
1 1 A FOX
1 2 B CAT
1 3 B CAT
1 4 B CAT
2 7 A DOG
2 6 A DOG
2 8 B MOUSE
2 9 B MOUSE
I have T1 T2 T3 where I wish to UNION T1 and T2 and afterwards JOIN T3 such as:
SELECT * FROM T1
UNION
(SELECT * FROM T2)
JOIN
(SELECT SCHOOL, TEACHER, ID
WHERE SCHOOL != 10
FROM T3)
BUT I receive error "UNEXPECTED JOIN"
JOIN is a part of a SELECT statement while UNION are not.
So, you need to make UNION as a part of a SELECT statement
select * from (
SELECT * FROM T1
UNION
SELECT * FROM T2) q1
JOIN
(SELECT SCHOOL, TEACHER, ID
FROM T3
WHERE SCHOOL != 10) q2
on /* here goes JOIN condition you need, like q1.school = q2.school*/
Please note:
there is another syntax error in your example: FROM goes always after SELECT and before WHERE
UNION will append result and eliminate duplicate rows which can run slower than UNION ALL. The latter will not check whether there are duplicate rows. So, if you're sure there won't be any duplicates in the result or if it's irrelevant whether you get duplicates or not, you may use UNION ALL
I have the following tables
t1
id
stage
1
1,2,3
2
2,3,4
t2
id
t_id
stage_id
1
1
2
2
1
1
3
1
3
4
2
2
5
2
4
6
2
3
I hope the result can first order by t2.t_id and then order by the value of t1.stage
like the following result
t_id
stage_id
1
1
1
2
1
3
2
2
2
3
2
4
I have the following sql ,but it do not work.So what should I do?
SELECT
t2.t_id,
t2.stage_id
FROM
t2
LEFT JOIN t1 ON t1.id = t2.t_id
GROUP BY
t2.t_id,
t2.stage_id
ORDER BY
t2.t_id,
field(t2.stage_id, t1.stage)
FIELD() requires each value to sort by to be a separate argument.
FIND_IN_SET() returns the index in a comma-separated string.
SELECT
t2.t_id,
t2.stage_id
FROM
t2
LEFT JOIN t1 ON t1.id = t2.t_id
GROUP BY
t2.t_id,
t2.stage_id
ORDER BY
t2.t_id,
find_in_set(t2.stage_id, t1.stage)
Note that this only corrects the ordering criteria in your SQL. This won't necessarily return the first row in each group by that ordering (and will get an error if the ONLY_FULL_GROUP_BY SQL mode is enabled). See SQL Selecting from two Tables with inner join and limit for the correct way to do that.
I have a table where exists 4 entries like this.
class_type
id type
1 A
2 B
3 M
4 T
and another table where these values are foreign key.
id number id_class_type
1 10 1
2 11 1
3 12 2
4 13 1
5 14 2
6 15 3
7 16 1
8 17 3
So what i want is count(*) and group by id_class_type but with all class_type (1,2,3,4) although there is not present the id 4.
if you want only the class tha match you can use inner join
select a.class_type, count(*)
from class_type a
inner join table2 b on a.id = b.id_class_type
group by a.class_type
otherwise you can use left join
select a.class_type, count(*)
from class_type a
left join table2 b on a.id = b.id_class_type
group by a.class_type
Can we use a join operation and make restrictions for different columns at the same time? I want to create a table on a Database (called DB1 in the example) based on three tables from another database (called DB2 in the example) where some columns in the new table filled with entrys when there´s a specific entry in an other column (in the example a"4" in column "attributeID" at table 2 indicates an entry in column "gender", a "5" indicates an entry in column "age"; a"7" in column "attributeID"at table 3 indicates an entry in column "street"). -> If yes, then how to do ?
Both databases are on the same server and DBMS is the same. ID1 and ID2 based on table1 in DB2; ID2, attributeID and value (where the gender or age is written depending on entry in attributeID) based on table2 in DB2; ID2 attributeID and value (where the street is written when the entry in the attributeID is a 7)based on table3 in DB2 .
Sample Data:
Table 1:
ID1 ID2
1 2
2 4
3 5
Table 2:
ID2 attributeID value
2 3 Kahtrin ->an example entry which is not relevant
2 4 miss
2 5 22
4 1 active ->an example entry which is not relevant
4 4 EMPTY/NULL
4 5 47
5 4 mr
5 5 34
5 6 Hindu ->an example entry which is not relevant
Table 3
ID2 attributeID value
2 5 20% ->an example entry which is not relevant
2 7 middlestreet 40
4 4 chinese ->an example entry which is not relevant
4 7 churchstreet 12
5 3 3Euro
5 7 EMPTY/NULL
Expected Outcome
Table 4:
ID1 ID2 gender age street
1 2 miss 22 middlestreet 40
2 4 47 churchstreet 12
3 5 mr 34
Here´s what I tried out (Made from point of view that I´m using DB1):
INSERT INTO table4
(id1,
id2,
gender,
age,
street)
SELECT t1.id1,
t1.id2,
t2.value
t2.value
t3.value
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID2=t2.ID2
AND value = 4 OR 5
LEFT JOIN db1.table3 t3
ON t1.ID2=t3.ID2
AND value = 7;
You select is wrong (don't produce the expected result ) you should use two time the table t2 using an alias
and you should explicit assign the table t3 to value
SELECT t1.id1,
t1.id2,
t2.value
t4.value
t3.value
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID2=t2.ID2
AND value = 4
LEFT JOIN db1.table2 t4
ON t1.ID2=t4.ID2
AND value = 5
LEFT JOIN db1.table3 t3
ON t1.ID2=t3.ID2
AND t3.value = 7;
I have a table with only an id field, I would get the result of this field, as distintic id and another column all have different IDs and can not be equal and also results already found previously...EX:
Id_field
1
2
3
I want the following result:
1 - 2
1 - 3
2 - 3
I d'nt
1 - 1
2 - 2
3 - 3
and result previously stated
2 - 1
3 - 1
3 - 2
Simple self join?
SELECT a.id_field, b.id_field
FROM SomeTable a
INNER JOIN SomeTable b
ON a.id_field < b.id_field
SELECT t2.id AS id2,t1.id AS id1
FROM t AS t1
JOIN t AS t2 ON (t1.id > t2.id);
SQLFIDDLE