how distinct and different results of the same field, differentiating rows - mysql

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

Related

How can I get the result from mysql after group and order

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.

Identify missing conversions with an SQL query

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

how to remove duplicate entries from many to many relation using sql query

My tables look like this. my op and country is having many to many relationships with each other.
OP
id, name,.....
op_country
id, op_id, country_id
country
id, name, ...
my op_country filled like below
id op_id country_id
1 1 1
2 1 2
3 2 2
4 2 3
5 3 3
6 3 3
7 1 1
I want to remove my duplicate entries from op_country. Here I want to remove rows 6 and 7 since we already have rows with such values.
How can I do that.
DELETE t1
FROM op_country t1
JOIN op_country t2 USING (op_id, country_id)
WHERE t1.id > t2.id
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=247ebc5870a6ab10b64076ffb375797f
You want to delete entries for which exists a sibling with a lower ID:
delete from op_country
where exists
(
select null
from (select * from op_country) op2
where op2.op_id = op_country.op_id
and op2.country_id = op_country.country_id
and op2.id < op_country.id
);
The from (select * from op_country) is necessary instead of a mere from op_country due to some weird restriction in MySQL updates.

query to get record based on joined table column array

i have a query that its result is array like $a=[1, 3, 5]
i need another query which return records from table1 that all b values are in $a=[1,3, 5] so result for this sample is table1.id=1, 2
can i implement this with a query or i have to use php code like array_diff() to check difference between b column and $a?
**table1**
id
-----------
1 ...
2 ...
3 ...
**table 2**
table1_id b
------------
1 1
1 3
2 1
3 1
3 4
4 1
4 3
4 5
4 4
If i understand it correctly:
SELECT * FROM table2 WHERE table2.b IN (SELECT id FROM table1 WHERE YourCondition)
If you want to join multiple tables, with matching id for example, use:
Select * from table1 inner join table2 on table1.id=table2.table1_id

Sql join, extract value from the second table and add it to the first one

I have 2 tables in Mysql. I need to join them somehow to get one value from second table into the first one.
TABLE 1
Day EmployeeId Total EmployeeName
1 2 20 Josh
1 1 20 Mike
2 2 5 Josh
2 1 10 Mike
3 3 5 Eric
TABLE 2
Day EmployeeId Max_Total
1 2 40
1 1 40
2 2 5
2 1 15
I need to get something like TABLE 3
Day EmployeeId Total EmployeeName Max_Total
1 2 20 Josh 40
1 1 20 Mike 40
2 2 5 Josh 5
2 1 10 Mike 15
3 3 5 Eric null
So this Max_Total column needs to be somehow created and populated.
This Day_EmployedId combination is unique in both tables and that should be used somehow to extract values from 2nd table and add it to the first one.
Sometimes first table can have more values, sometimes the second one, but the first one will always be the one that needs to be manipulated/added to.
Any hint will be appreciated. Thanks
You are looking for a left join on two fields:
select t1.*, t2.max_total
from table1 t1 left join
table2 t2
on t1.day = t2.day and t1.employeeid = t2.employeeid;
I would not recommend actually updating table1. You can generate the data as you need it. However, in order for an update to work, you need to add a column to the table first, and then update it.
You need to separate your tasks.
Alter your Table1 to add the column Max_Total
Write UPDATE query to update your Max_Total in your Table1.
Query:
UPDATE t1.Max_Total = t2.Max_Total
SET t1.
FROM Table1 t1
JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
If you are only concerned about getting a combined result set
SELECT t1.Day, t1.EmployeeId, t1.Total, t1.EmployeeName, t2.Max_Total
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
For more information on LEFT JOIN, you can study this tutorial.