Select from 2 database remove same column - mysql

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
)

Related

Once I compare two tables how can I update the first table?

Once I have the two tables compared and they give me what has changed, I would like the first table to be updated with the new data from the second one.
This is my code where I put in my phpmyadmin database that compares the two tables:
SELECT
codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM (
SELECT codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM operatore
UNION ALL
SELECT codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM operatoreImport
) tbl
GROUP BY codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
HAVING count(*) = 1
ORDER BY codice_Fiscale
I have this tables:
TABLE 1:
id name
1 Oriol
2 Ricard
TABLE 2:
id name
1 Uriol
2 Ricard
And once compared the two tables, I want this:
TABLE 1:
id name
1 Uriol
2 Ricard
maybe this code help you
UPDATE table1 t1, table2 t2
SET t1.name=t2.name, t1.surname=t2.surname
WHERE t1.id=t2.id

Single row/single columns query (MySQL)

I have this table:
datatable
I would like to write a single query to update whole 99 by below number so for each 99 will be replaced by number below, for each blank name, the name below.
I tried:
update datatable.code set datatable.code= previous (datatable.code)
WHERE datatable.code=99;
update abc set code =
CASE WHEN name='Jean' THEN 1
WHEN name='Peter' THEN 2
END
where code=99;
use mysql without update
select *,
if(ta.code!=99,ta.code, select ta2.code from table_a as ta2 where ta2.ID_q=ta.ID_q and ta.code!=99 limit 1) as code_a,
if(ta.name!='',ta.name, select ta2.name from table_a as ta2 where ta2.ID_q=ta.ID_q and ta.name!='' limit 1) as name_a
from table_a as ta;

MySql using AS to change field name in PHP

I have a table with the following field idd. It is however screwing up my code as I have to rename everything from array['id'] to array['idd']. Is there anyway to fix this? I tried the following!
select * from table where (idd as id) = 2
select *,idd as id from table where id = 2
Try this one
SELECT column1, column2, ..., idd AS id, columni, columni+1, ...
FROM yourTable
WHERE id=2

MySQL: Insert using results of Select field from pair clause

I've got a requirement to add an additional item of data to an existing row and insert the result in a second table. The data item is different for each row I am selecting, so I can't just add it to the SELECT statement. The original query is:
SELECT player_id,token_id,email FROM players
WHERE token_id in (101,102) OR email in ("test4#test.com");
I'd like to be able to do something like a Row Constructor and write the query something like this:
SELECT player_id,token_id, email, key_val FROM players
WHERE (token_id, key_val) in ( (101, 'xyz'),(102,'abc'))
OR (email, key_val) in ( ("test4#test.com", 'qpr') );
So that the second value ('key_val') from the pair in the IN clause would be added into the SELECT output as the last column. And then the whole lot will get inserted into the final table.
The number of items in the IN clause will vary from 3 to potentially 100's.
Really sorry if this is a dup. I've looked up things like:
Select Query by Pair of fields using an in clause
MySQL: How to bulk SELECT rows with multiple pairs in WHERE clause
I guess I could use a temporary table but I'm concerned about the number of times that this is going to be called.
Edit--
To clarify, the source table is something like:
player_id, token_id, email
===================================
1 101 null
2 102 null
3 null test4#test.com
and the date being supplied is:
(token_id=101, key_val='xyz'),(token_id=102, key_val='abc'),(email='test4#test.com', key_val='qpr')
and the intended output would be:
player_id token_id email keyy_val
========== ========= ============== ========
1 101 null zyz
2 102 null abc
3 null test4#test.com qpr
Hope this makes it clearer.
try this
SELECT player_id,token_id, email, key_val
FROM players
WHERE token_id in (101,102) AND key_val IN ('xyz','abc')
OR ( email in ("test4#test.com") AND key_val IN ('qpr') );
EDIT -.
try this
SELECT player_id,token_id, email, key_val
FROM ( select player_id,token_id, email,
if(`token_id` =101 , 'xyz',
if(`token_id` =102 , 'abc' ,
if(email = "test4#test.com" , 'qpr' , NULL))
) key_val
from players
)p
DEMO SQLFIDDLE

Insert multi column result into single column table

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