SQL query, 2 tables, avoid duplicated results - mysql

Table 1
NO_REG - ID
Table 2
NO_REG - BAGS
the problem its that table 1 contains lets say 3 ID, table 2 has the same NO_REG but just 1 row in BAGS
i do
SELECT BAGS, ID FROM TABLE 1 AS T1 INNER JOIN TABLE 2 AS T2 ON T1.NO_REG = T2.NO_REG
I get
NO_REG ID - BAGS
123 999- 2
123 989- 2
123 979- 2
I need
NO_REG ID - BAGS
123 999- 2
123 989- NULL (or 0)
123 979- NULL (or 0)
hope i was clear enough.

You need LEFT JOIN instead of INNER JOIN which retrieves matching values and null for non matching
Just something like
SELECT BAGS, ID FROM TABLE 1 AS T1 LEFT JOIN TABLE 2 AS T2 ON T1.NO_REG = T2.NO_REG

Related

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.

How to get the values returned from a a group by aggregate query as zero, (0), when a count of the field is zero?

I would like to get a value returned (in this case a zero) from the query for types in the event that a count of Table.id is zero.
SELECT COUNT(Table1.id) AS AccountID, Table2.typeName AS Types
FROM Table1
LEFT JOIN Table3
ON Table1.productID = Table3.productID
LEFT JOIN Table2
ON Table3.categoryID = Table2.CategoryID
WHERE Table3.account_id = 51
GROUP BY Table2.typeName;
This is the result that I would like to get
This is the result that I am currently getting
Table1
id ProductID
1 1
1 1
2 2
2 1
1 1
id in this table 1 references categoryID in table2
---------------------------------------------------------
Table2
categoryID typeName
1 SUV
2 BMW
3 Toyota
4 Audi
5 Suzuki
---------------------------------------------------------
Table3
categoryID ProductID account_id
2 1 51
1 2 52
3 1 51
categoryID is table3 references categoryID in table2
----------------------------------------------------------
My expected result should look like this
Count Types
0 Toyota
0 Audi
0 Suzuki
3 SUV
2 BMW
I want a count of all the id(s) from table1 with its corresponding typeName. If the id is not present, I want the a 0 to be returned with the corresponding typeName.
Following query will work:
SELECT
COUNT(Table1.id) AS AccountID,
Table2.typeName AS Types
FROM Table2
LEFT JOIN Table3
ON Table3.categoryID = Table2.CategoryID
AND Table3.account_id = 51
LEFT JOIN Table1
ON Table1.id = Table3.CategoryID
GROUP BY Table2.typeName;
Click here for DEMO

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

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

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