sql join issues with different data - mysql

I want to join two tables with different data and data type is same.
In tableA the column col1 is with varchar datatype i.e. 123 and in tableB the column col1 is with varchar datatype i.e. ABC-123
Is there any way to join both columns by adding ABC as prefix to col1 in table 1 or by removing prefix ABC from col1 table 2.

You can use CONCAT(), as in:
select *
from table_a a
join table_b b on concat('ABC-', a.col1) = b.col2
This issue is quite common, specially in old databases, where you need to join VARCHAR columns with NUMERIC ones, since the designers back in the 90s though it that way.

Just use function CONCAT() at the ON clause of INNER JOIN
select * from
tableA a inner join tableB b
on CONCAT('ABC-', a.col1) = b.col2

Related

Return rows from MYSQL table A where table_A.col_1 = table_b.col_2 without using a left outer join?

In a MySQL database with two tables table_A and table_B I want to return selected row columns from table_A based on comparison with values in table_B. The below erroneous line sums up the idea:
SELECT col_1, col_2, col_3 FROM table_A where table_A.col_1 = table_B.col_2;
I do not want any elements from table_B.
Why I can't use a left outer join: I've tried this with a left outer join as illustrated here(https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/) however the database complains that the column names are ambiguous and changing table column names in the database isn't an option.
I you want rows in table_A whose col1 can be found in table_B(col_2), you can use exists:
select a.col_1, a.col_2, a.col_3
from table_A a
where exists (select 1 from table_B b where b.col_2 = a.col_1);
If you want rows that do not exist in table_B, then just change exists to not exists.
Note that I prefix the column names with the (alias of the) table they belong to. This is called qualifying the columns, and is how you avoid the ambiguous column name problem that you seemingly have met when trying to join.
If column names are ambiguous, qualify them, eg
select table_A.col_1, table_A.col_2, table_A.col_3
from table_A
join table_B on table_A.col_1 = table_B.col_2
or for brevity you can assign an aliases to tables:
select a.col_1, a.col_2, a.col_3
from table_A a
join table_B b on a.col_1 = b.col_2

Join/Update between Three Tables

Currently to get data updated in Table3 that depends on conditions in Tables1 and Table2 I am doing this:
Update Table_B as T1
Inner Join Table_A as T2
On T1.S_ID=T2.ID
Set T1.Percent = T2.Percent
Update Table_C as T1
Inner Join Table_B as T2
On T1.ID=T2.J_ID
Set T1.B = T2.B
Where T2.Percent=100
I would like to not store or update TableB with TableA Percent and somehow do the join in a single statement.
update table_b T1 set T1.Percent=(select T2.Percent from table_a T2 where T1.S_ID=T2.ID)
update table_c T1 set T1.B=(select T2.B from table_b T2 where T1.ID=T2.J_ID) where exists(select * from table_b T3 where T1.ID=T3.J_ID and T3.percent=100)
This isn't valid syntax in an UPDATE statement
Set T1.Percent as T1.Percent
^^
And this isn't valid
Set T1.B as T2.B
^^
The syntax in the UPDATE statements in the question do not look valid.
The SET assignment operator is an equals sign, not an As keyword.
And T1.Percent = T1.Percent doesn't make much sense, since there doesn't seem to be any point to setting a column to its current value.
It's not clear what it is you are actually trying to achieve, without some example data, and the desired end result.
To write a multi-table UPDATE statement, I first write a SELECT statement that returns the primary (or unique) keys of the tables that I am going to update, along with the current values of the columns to be updated, and expressions that return the new values that are going to be assigned to the columns
Based on the SQL in the question, it looks like you might want two joins, something like this:
SELECT ...
FROM table_a a
JOIN table_b b
ON b.s_id = a.id
JOIN table_c c
ON c.id = b.j_id
WHERE a.percent = 100
ORDER BY ...
(We don't know anything about the cardinality of the columns and relationships, whether those are one-to-one, one-to-many, zero-or-one-to-one, etc. We're just guessing.)
To evaluate and test the query, I'd include the primary keys of the tables. I'm going to guess id is the primary key of (the unfortunately named) Table_A and Table_C. And guess that the tuple (j_id,s_id) is a UNIQUE KEY in Table_B.
I will also include the column to be updated (to show the current value), and an expression that returns the new value to be assigned to the column.
Something like this:
SELECT a.id AS `a__id`
, a.percent AS `a__percent`
, b.j_id AS `b__j_id`
, b.s_id AS `b__s_id`
, c.id AS `c__id`
, c.b AS `c__b_old`
, b.b AS `c__b_new`
FROM table_a a
JOIN table_b b
ON b.s_id = a.id
JOIN table_c c
ON c.id = b.j_id
WHERE a.percent = 100
ORDER
BY c.id
, a.id
I would test that, and make sure it's returning the rows from Table_C that I want to update, and that the value returned for the c__b_new expression is the value that I want to assign to the b column in Table_C.
Once I get the SELECT statement working correctly (and only after I get it working correctly), I convert that to a multitable UPDATE.
Just replace the SELECT ... FROM part with the keyword UPDATE.
And before the WHERE clause, add a SET clause that does the assignment of the new value to the column. (I use the same expression I used in the SELECT list to return the new value.)
UPDATE table_a a
JOIN table_b b
ON b.s_id = a.id
JOIN table_c c
ON c.id = b.j_id
SET c.b = b.b
WHERE a.percent = 100
That's just an example of how I go about writing a multi-table UPDATE. There is no guarantee that this statement is going to work for what you need. Again, it's not clear what you are trying to achieve; the specification is too vague; so we're only guessing.

Transforming a Complicated Requirement into a SQL Query

I am having trouble with the relational algebra and transformation into SQL of this rather complicated query:
I need to select all values from table A joined to table B where there are no matching records in table B, or there are matching records but the set of matching records do not have a field that contains one of 4 of a possible 8 total values.
Database is MySQL 5.0... using an InnoDB engine for the tables.
Select
a.*
from
a
left join
b
on
a.id=b.id
where
b.id is null
or
b.field1 not in ("value1","value2","value3","value4");
I'm not sure if there is any real performance improvement but one other way is:
SELECT
*
FROM
tableA
WHERE
id NOT IN ( SELECT id FROM tableB WHERE field1 NOT IN ("value1", "value2"));
Your requirements are a bit unclear. My 1st interpretation is that you only want the A columns, and never more than 1 instance of a given A row.
select * from A where not exists (
select B.id
from B
where B.id=A.id
and B.field in ('badVal1','badVal2','badVal3','badVal4')
)
My 2nd interpretation is you want all columns from (A outer joined to B), with perhaps more than one instance of an A row if there are multiple B rows, as long as not exists B row with forbidden value.
select * from A
left outer join B on A.id=B.id
where not exists (
select C.id
from B as C
where A.id=C.id
and C.field in ('badVal1','badVal2','badVal3','badVal4')
)
Both queries could be expressed using NOT IN instead of correlated NOT EXISTS. Its hard to know which would be faster without knowing the data.

Handling Ambiguous Column Names

Im in a position where I cannot alter the table structure of my database and I have Ambiguous Column Names in [table1] and [table2]. I do not need to use any fields from [table2] but its existence is necessary to relate to another table. Is there a way that I handle this?
Every time you refer to one of the ambiguous column names you should specify the table name or alias.
SELECT ...
FROM [table1]
JOIN [table2]
ON [table1].ambiguous_column = [table2].ambiguous_column
AND ...
use table aliases
SELECT A.*
FROM TABLE_A A
JOIN TABLE_B B ON A.ID = B.ID
ORDER BY A.FIELD
use the SQL statement AS to create uniquel names
SELECT
A.feld1 AS F1,
A.feld2 AS F2,
B.feld1 AS F3
FROM table1 AS A
JOIN table2 AS B ON A.id = B.id
ORDER BY A.field1

How to select content from two different tables in Mysql?

How can I select the contents of two columns that reside in different tables in a mysql database?
You would need to use either a JOIN or UNION/UNION ALL.
This will depend on wht you require.
Lets say you want all values from table 1 col a and table 2 col b in seperate rows
You can use
SELECT ColA
FROM TABLE1
UNION ALL
SELECT ColB
FROM TABLE2
All Distinct Values
SELECT ColA
FROM TABLE1
UNION
SELECT ColB
FROM TABLE2
And lets say that the you want to display them in the same row, they should have some key that links them
SELECT ColA, ColB
FROM TABLE1 t1 INNER JOIN
TABLE2 t2 ON t1.ID = t2.ID
It would also be good to note that there are different types of Sql Joins
Different SQL JOINs
JOIN: Return rows when there is at
least one match in both tables
LEFT JOIN: Return all rows from the
left table, even if there are no
matches in the right table
RIGHT JOIN: Return all rows from the
right table, even if there are no
matches in the left table
FULL JOIN: Return rows when there is
a match in one of the tables
Use a JOIN.
http://w3schools.com/SQL/sql_join.asp
SELECT fields
FROM table_a a
JOIN table_b b
ON (a.id = b.foo_id)