Comparing table B to table A? - mysql

select A.`Product#`,
a. ` Email Address`
FROM
Table a A, `Table b B
where A.`Product#`<> B.`Product#`
I am trying to compare if Table B's product# exists in Table A or not, this query does not give 100 % result.
Please tell me what is wrong in this query.

You should use a NOT EXISTS query
select *
FROM
Table b B
where not exists ( select 1 from table a A where A.`Product#`= B.`Product#`)
This will show you all that exists in table B that does not exist in table A which is what I believe you were asking in your question. Can be reversed to show what's in table A that is not in table B.

Related

Selecting columns from different tables while using join statements in MySQL; using database name in SQL script [duplicate]

This question already has answers here:
1052: Column 'id' in field list is ambiguous
(8 answers)
Closed 1 year ago.
I am a beginner in SQL queries/subqueries and I'm having a lot of problems with my code. I have two tables, TableA and TableB. My database is called data.
Contents of tables:
Table A:
IDA (primary key)
ColumnA1
Table B:
IDB (primary key)
IDA (foreign key)
ColumnB1
ColumnB2
I am attempting to select IDA from TableA and also select a new column I created called NewColumnSubtract which was created as a result of a join between TableA and TableB. The result I want displayed is IDA and NewColumnSubtraction.
Here is an example of my code:
use `data` ;
SELECT
IDA,
ColumnA1 - (ColumnB1 * ColumnB2) AS NewColumnSubtraction
FROM
`data` . TableA
JOIN
`data` . TableB ON TableB.IDA = TableA.IDA;
If I do not include the third line of my code, which is just selecting IDA first, it works and just selects the new column I created. The problem is that I also want to display IDA.
Another problem I am having is that if I do not include 'data' . Table_ (there is no space before and after the . in my actual code), I get an ambiguous error. I know it is bad practice to call the database name in the script, but I do not know how to get around this.
Any help here would be greatly appreciated. Thank you so much!!
You have to explain to the database which of the IDA columns you want.
You can write the table name before the column or use aliases, so that you must not write that much.
If you have more columns like that , you need also to add the table name or alias.
&To prevent such problems, rty to make unique column names
use `data` ;
SELECT
a.IDA,
ColumnA1 - (ColumnB1 * ColumnB2) AS NewColumnSubtraction
FROM
TableA a
JOIN
TableB b ON b.IDA = a.IDA;
example
CREATE TABLE TableA (IDA int, ColumnA1 int)
CREATE TABLE TableB (IDA int, ColumnB1 int, ColumnB2 int)
SELECT
a.IDA,
ColumnA1 - (ColumnB1 * ColumnB2) AS NewColumnSubtraction
FROM
TableA a
JOIN
TableB b ON b.IDA = a.IDA;
IDA
NewColumnSubtraction
db<>fiddle here
There should not be any syntax error. Please check the query again. Here goes an example.
Create table statements:
create table TableA(IDA int, ColumnA1 int);
create table TableB(IDB int, IDA int, ColumnB1 int,ColumnB2 int);
Query:
SELECT
a.IDA,
(a.ColumnA1 - (b.ColumnB1 * b.ColumnB2)) AS NewColumnSubtraction
FROM
TableA a
JOIN
TableB b ON b.IDA = a.IDA;
Output:
IDA
NewColumnSubtraction
db<>fiddle here
I found the solution by doing this:
use data ;
SELECT
TableA.IDA,
TableA.ColumnA1 - (TableB.ColumnB1 * TableB.ColumnB2) AS NewColumnSubtraction
FROM
data . TableA
JOIN
data . TableB ON TableB.IDA = TableA.IDA;
So, I had to not only call the database for each instance of a table but the table for each instance of a column. Anyone have any ideas as to why this is happening?

Create a temporay table with more columns than specified in the SELECT statement

I have created a temporary table from another table and it works well. However, I'd like to add a column to the temporary table and then SELECT ALL from the existing table(unoone). Please any insight and thanks upfront
$sql="CREATE TEMPORARY TABLE IF NOT EXISTS unoone_two AS (SELECT * FROM unoone)";
enter code here
You can add the columns to the select:
CREATE TEMPORARY TABLE IF NOT EXISTS unoone_two AS
SELECT u.*, NULL as newcol, CAST(NULL as SIGNED) as newcol2
FROM unoone u;
You can specify a type by using CAST().

updating records in table A from table B if column XYZ ( checksum ) changes in table B

I am having a table A and table B.
Table A is created from Table B ( and few other table join operation ).
Table A has all of its column which are subset of column in table B.
There is a column called as check_sum in table A and table B. This is basically a calculated column and if any column value changes then check_sum ( calculated value ) changes as well.
For example:
Table A ( schema ):
cust_id ( pk ), cust_name, cust_location, check_sum ... other columns ( no need to worry about them )
Table B ( schema ) :
cust_id ( pk ), cust_name, cust_location, check_sum
Initially table B and A have entries like below:
Table A: ( sample record )
1, John, USA, abc222abc, ... other columns
Table B: ( sample record )
1, John, USA, abc222abc
Now lets say John changes his country location to UK, then corresponding entry in TABLE A looks like this
Table A: ( sample record )
1, John, UK, checkSumChanged, ... other columns
Now i need to update my table B accordingly, so that instead of location of John as USA it should have it as UK.
Column checksum is helpful here, since its value changes in Table A if any column changes.
This is the part i am stuck at. Not able to update just "CHANGED" rows from Table A to Table B. I have following query below. It is updating all rows instead of just the changed rows.
Here is the query.
UPDATE tableB
SET
tableB.cust_name = tableA.cust_name,
tableB.cust_location = tableA.cust_location,
tableB.check_sum = tableA.check_sum
FROM
tableA inner join tableB
on tableA.cust_id = tableB.cust_id
and tableA.check_sum != tableB.check_sum
Any ideas or suggestion how can i correct my query to just update the changed record.
Thanks in advance!!!
Do it without a join:
update B
SET cust_name = a.cust_name, checksum = a.checksum, cust_location = a.cust_location
FROM a
WHERE a.custid = b.custid AND a.checksum != b.checksum;

Updating Table Rows with another table data

I have two tables I am trying to update table A data with table B. Table A (ids) has 100 rows (based on a condition) and I want to replace those ids with table B(ids).
Table A
abc
def
klm
ijk
Table B
abc1234
pknm
lokimh
2546njnh
can you please provide me the SQL to replace these ids form table B to table A
there are 100 rows which i need to update from table B to table A.
Cursor trans
select id
from table B
where rownnum <=100
cursor user
select id
from table A
update tab_a set colname =
(select colname from tab_b where -- condition, use the condition you are referring to )

Move data from one table to other in mysql and drop the table

I want to write a Mysql query for the following scenario.
1.check if a table( ex: tableA) exists.
2.Check if data is there in the table.
3.If tableA exists and data is there in the table move all data to another table( ex: tableB) (tableB there in db and both tables are having same structure)
4.drop tableA
Is it possible to write a mysql query avoiding mysql stored procedure ?
I was able to do first three with the below query, but drop table was not possible.
Hope it helps!
There are two tables: table_1(old),table_2(new). Both have one column "ID".
insert into table_2(id) #inserts into table
select case when
(
select count(*) from table_1 a #checks if there are any records in the table
where exists #checks if table exists
(select 1 from table_1 b where a.id=b.id))>0 then
id
else 0 end
from table_1