Mysql join on similar columns - mysql

I want to join table1 with table2 on column 'Name', but table2.Name has an 'e' in front of all the names (if table1.name=ABC,table2.name=eABC). How am I supposed to use a join for those two?
I tried FROM table1 join table2 on 'e'+table1.name = table2.name, but it doesn't work...

SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.name = CONCAT('e', t1.name)

Try using a substring of the table2 name. So something like:
SELECT *
FROM table1
, table2
WHERE table1.name = substring(table2.name, 1, length(table2.name))
I can't remember if substring is zero based, so just play with the numbers.

Related

how to return all the fields of table2 based upon the occurrence of the id in the table1

I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;

SQL query to find unreferenced records

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

How to select all columns from one table, but only selected columns from the other?

How to select * from the first table and only x column from the joined table?
SELECT *(TABLE1), TABLE2.id, TABLE2.name
FROM TABLE1
JOIN TABLE2
ON TABLE1.id_test = TABLE2.id
have not tested it but it should look like this:
SELECT t1.*, t2.id, t2.name
FROM TABLE1 t1
JOIN TABLE2 t2
ON t1.id_test = t2.id
SELECT table1.*, table2.id, table2.name
FROM table1
JOIN table2
ON table1.id_test = table2.id
like this:
SELECT TABLE1.*, TABLE2.id, TABLE2.name
FROM TABLE1
JOIN TABLE2
ON TABLE1.id_test = TABLE2.id
though do make sure you really want that * since if you don't want all columns that's a memory waster.

MYSQL: How to make it virtual column in select Query?

Below is the my question any one have idea how to resolve this issue:
Example Query:
SELECT id, table2.id as global_id, table3.id as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc
Basically i don't want to write two times "global_id" column in the select statement and also in the some rows have id = NULL
Can you please write working query here. How can i get id value of all rows in the global_id column ?
Thanks
Use COALESCE:
SELECT table1.id, COALESCE(table2.id, table3.id) as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc
Guess you need this:
SELECT id, table2.id as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
WHERE etc etc
UNION ALL
SELECT id, table3.id as global_id
FROM table1
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc

MYSQL join + replace?

need help building a query to replace fields on a table that match a second table.
table1
city
table2
city | code
I need replace all occurrences of city on table1 by code of the table2 matching the city field
i think it'll be something like this:
update table1 t1 set t1.city = t2.code from table2 t2 where t1.city =
t2.city
Actually, not necessary for a replace:
UPDATE table1 JOIN table2 ON table1.city = table2.city SET table1.code = table2.code