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!!