MySQL query to join two table having different columns - mysql

Consider the below table T1
FLOOR...ANIMAL...COLOUR
1...Cat...Black
1...Dog...Black
1...Cat...White
2...Dog...White
2...Dog...Brown
3...Cow...Brown
3...Dog...White
3...Cat...Black
Now, I with query: Select * from T1 where animal='cat'; got this Table T2
Table T2
FLOOR...ANIMAL...COLOUR
1...Cat...Black
1...Cat...White
3...Cat...Black
The result I want is: All the elements present in Table T2 and only(FLOOR,ANIMAL) of Table T1. Something like the below Table T3:
Table T3
FLOOR...ANIMAL...COLOUR
1...Cat...Black
1...Cat...White
3...Cat...Black
1...Dog
2...Dog
2...Dog
3...Cow
3...Dog
How this Table T3 can be acheived? Any suggestions?
Thanks in advance.

You could try taking the UNION of two TABLES wherein the second TABLE should have its third column as empty string:
select * from T1 where animal = 'cat'
UNION
select floor, animal, " " as color from T1 where animal <> 'cat';

You can try with case
like this :
case when ANIMAL = "cat" then COLOUR else '' end

Related

Value from one table on basis of another table

I have two table as table1 and table2 given below:
I want to have the value of only those table_name from table1 which has there id in print_table column in table 2.
I have implemented the following query but it returns only one value:
SELECT * FROM print_tabel_permission_admin WHERE id IN (select print_table from secondary)
Use FIND_IN_SET:
SELECT DISTINCT
t1.table_name
FROM table1 t1
INNER JOIN table2 t2
ON FIND_IN_SET(t1.id, t2.print_table) > 0;
Demo
You should probably move away from storing CSV data in your tables like this. Instead, break out the IDs in table2 onto separate rows. This will make your life easier.

Select same columns from multiple tables

I have a MySQL database with multiple tables and those tables contain multiple columns that are equal.
For example:
table1 contains productid, price, and a couple of columns specific for that table.
table2 contains productid, price, and a couple of different columns (not in table1)
table3 also has productid, price and more unique columns, etc etc.
Is it possible to select productid and price from all three tables in one query and get the result in one output? One way would be to select into some temporary table, but is there an easier/better/nicer way?
using the union :
select productid,price from table1
union select productid,price from table2
union select productid,price from table3
you can use aliases in your query, so you will be able to get different column, e.g:
select table1.productid as productId1, table1.price as price1, table2.prodcutid as productId2, ....
useing below query you can get record from different table
SELECT t1.productid, t1.price,t2.productid, t2.price,t3.productid, t3.price from table1 t1,table2 t2 ,table3 t3
If you need relevant data among all tables which having common productid then you should use joins like:
select t1.productid as t1_productid,t1.price as t1_price,t1.othercol
t2.productid as t2_productid,t2.price as t2_price,t2.anothercol
t3.productid as t3_productid,t3.price as t3_price,t3.manymorecol from table1 t1
inner join table2 t2 on t2.productid = t1.productid
inner join table3 t3 on t3.productid = t2.productid
let me know if you want another kind of output.
If you have many tables have same strucure using the this code:
Oracle
Select ' Select productid,price'||
' from '|| table_name ||
' Union' quer
From tabs
Where table_name like 'table%';
SQL Server
Select ' Select productid,price'+
' from '+ table_name +
' Union' quer
From INFORMATION_SCHEMA.TABLES
where table_name 'table%';
after that copy the result and remove last union and execute it

SQL Join 2 tables with almost same field

I need to join two tables in SQL. There are no common fields. But the one table have a field with the value krin1001 and I need it to be joined with the row in the other table where the value is 1001.
The idea behind the joining is i have multiple customers, but in the one table there customer id is 'krin1001' 'krin1002' and so on, in this table is how much they have sold. In the other table there customer is is '1001' '1002' and so on, and in this table is there name and adress and so on. So it will always be the first 4 charakters i need to strip from the field before matching and joining. It might not always be 'krin' i need it to work with 'khjo1001' also, and it still needs to join on the '1001' value from the other table.
Is that possible?
Hope you can help me.
You need to use substring:
ON SUBSTRING(TableA.Field, 5, 4) = TableB.Field
Or Right:
ON RIGHT(TableA.Field, 4) = TableB.Field
You can also try to use CHARINDEX function for join operation. If value from table1 contains value from table2 row will be included in result set.
;WITH table1 AS(
SELECT 'krin1001' AS val
UNION ALL
SELECT 'xxx'
UNION ALL
SELECT 'xyz123'
),
table2 AS(
SELECT '1001' AS val
UNION ALL
SELECT '12345'
UNION ALL
SELECT '123'
)
SELECT * FROM table1 AS t
JOIN table2 AS T2 ON CHARINDEX(T2.val, T.val) > 0
Use it as:
SELECT
*
FROM table t1
INNER JOIN table t2 ON RIGHT(t1.col1, 4) = t2.col1;

SELECT * FROM table1 LEFT JOIN table1.value AS table2

i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view

Select both ID columns when using UNION and GROUP BY

I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.