I am trying to write a SQL Query. I want to select all the records from table 1, with one condition.
If a Type is 'Prime' in Table 1, and that Semi+Prime combination exist in Table 2, only then we will select it from table 1.
For example, in this sample attached here, 4 and 6 (having type=prime) are there in table 2, so we consider it for output. 8( having type=prime) does not exist in table 2, so we dont take it in output.
This condition will be applicable only when Type is Prime in table1.
I have created the table here:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=13a5a2a6be51804a89ff5fd47edeeb70
Please try out below query it may help you:(Try it in your fiddle only)
First do the left join and using case condition remove non-prime:
select Group2,Type2,Group3 from t1 left join t2 on t1.type2=t2.group1 where (case when t1.group3 = 'PRIME' then t1.group3=t2.type1 else 1=1 end)
Try this query
select t1.Group1,t2.Semi,t1.Type from t1,t2 where t1.Type='PRIME' and t1.Semi=t2.Semi and t1.Type=t2.Type;
table t1:
table t2:
Output:
You can use Exists to get what you want.
create table t2
(Semi integer(10),
Type varchar(10)
);
create table t1
(Group2 varchar(10),
Semi int,
Type varchar(10)
);
insert into t2
VALUES(17,'PRIME'),
(4,'PRIME'),
(2,'PRIME'),
(3,'PRIME'),
(43,'PRIME'),
(34,'PRIME'),
(6,'PRIME'),
(23,'PRIME')
insert into t1
VALUES('A',1,'X'),
('A',2,'Y'),
('A',3,'Z'),
('A',4,'PRIME'),
('A',5,'X'),
('A',6,'PRIME'),
('A',7,'X'),
('A',8,'PRIME')
Query:
select * from t1
where type <>'PRIME'
or EXISTS
(
SELECT 1 FROM t2 WHERE t2.Semi=t1.Semi and t2.Type=t1.Type and t1.Type='PRIME'
)
Output:
Group2
Semi
Type
A
1
X
A
2
Y
A
3
Z
A
4
PRIME
A
5
X
A
6
PRIME
A
7
X
db<>fiddle here
Let's say we have a database with a table defined by the following fields:
id -- localCode -- localNumber -- status
Hoping to help you: there will be different records with the same combination (localCode + localNumber) but different status.
I would like to use the results from this query
SELECT localCode, localNumber FROM Table WHERE status = 'val_1'
to find the records with the combination (localCode + localNumber) found before but status = 'val_2'.
Thank you all
Make a self-join on the table.
SELECT
t2.id
FROM Table t1
INNER JOIN Table t2
ON t1.localCode = t2.localCode
AND t1.localNumber = t2.localNumber
WHERE t1.status = 'val_1'
AND t2.status = 'val_2'
This query will find all IDs of records that have status val_2 and where a corresponding entry with status val_1 exists.
I need to select all the values of table 1 from the first database that are not present in table 2 from the second database. I tried the code below, but DISTINCT does not work:
select DISTINCT(affected_ci),ci_name from sitequota.incidents,appwarehouse.ci_table where incidents.affected_ci <> ci_table.ci_name
DATABASE1: APPWAREHOUSE
TABLE1: CI_TABLE
COLUMN: CI_NAME
DATABASE2: SITEQUOTA
TABLE2: INCIDENTS
COLUMN: AFFECTED_CI
You could try something like:
SELECT ci_name
FROM appwarehouse.ci_table
WHERE ci_name NOT IN
(SELECT affected_ci FROM sitequota.incidents
)
I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'
I have a table 'movies' with three Columns: 'id', 'master_id' and 'searchMe' (simplified). I have another Table 'temp_ids' with a single column: 'id'. It is a temporary table, but I don't think that matters.
When I make a query on my table 'movies' like
SELECT `id`, `master_id` FROM 'movies' WHERE searchMe = '1';
I get a multi column result. Now I want to insert every id and every master_id into the 'temp_ids'-Table, but one at a time. So if my result is
id_1 | master_1
id_2 | master_2
id_3 | NULL
I want my temp_ids to look like
id_1
master_1
id_2
master_2
id_3
So I want to convert every single column in the result into its own row. How can I do that in an elegant way?
I know I can do it in multiple queries, searching for id and master_id separatly, and I know I can solve that problem with PHP or so. But I would prefer it to solve that problem in a single mysql-query, if such a thing is possible.
I made a sqlfiddle for this:
http://sqlfiddle.com/#!2/b4a7f/2
To SELECT the data you can use a UNION ALL for this:
SELECT `id`
FROM movies
WHERE searchMe = 1
union all
SELECT `master_id`
FROM movies
WHERE searchMe = 1
and master_id is not null
see SQL Fiddle with Demo
Doing it this way, you cannot distinguish between what value comes from each column, so you can always add an indicator, this will give you two columns but then you know where the data came from:
SELECT `id`, 'id' type
FROM movies
WHERE searchMe = 1
union all
SELECT `master_id`, 'master'
FROM movies
WHERE searchMe = 1
and master_id is not null
Then you would just use this query to INSERT INTO temp using this SELECT
It would be like this
INSERT INTO temp_ids(id)
SELECT id
FROM
(
SELECT id
FROM FirstTable
UNION
SELECT master AS id
FROM SecondTable
) t