Join 2 tables with different values - mysql

I have 2 MySQL tables: table1 and table2
The field "table1.name" has records like "category.1298" where the number after the dot comes from the field ID in table2.
I would like to join table2.ID with table1.name to identify whether table2.ID is equal to the number (after the dot) in table1.name
The question is how to eliminate the portion "category." in table1.name

substring_index is what you're looking for:
SELECT *
FROM table1
JOIN table2 ON SUBSTRING_INDEX(table1.id, '.', -1) = table2.id

Use substring_index
Select substring_index(table1.name,'.',-1) returns 1298
select *
from table1, table2
where substring_index(table1.name,'.',-1) = table2.id

Related

How to display records from MYSQL DB that are not empty?

I have two tables in a database table1 and table2
First of all I have used the INNER JOIN to obtain values from table1 and table2.
SELECT table1.id, table1.name,
table2.location, table2.address
FROM table1
INNER JOIN table2 ON table1.id=table2.id;
This query works just fine but the thing is that the address column in table2 table2.address contains empty/no values. Which I don't want to fetch. How to customize this query?
Furthermore,
I also want to filter a specific location table2.location.
For example, in simple words I want that when the results are fetched the location has to be "London" and addresses that are non-empty.
SELECT table1.id, table1.name,
table2.location, table2.address
FROM table1
INNER JOIN table2 ON table1.id=table2.id
WHERE table2.address IS NOT NULL
AND table2.location = 'London'
;

Mysql query based on row value of another table

Have two mysql tables such as
Table1
Id
Field1
Field2
Table2
Id
LinkId
Field3
Field4
The common fields to link the tables are Table1.Id and Table2.LinkId.
Also important tha Table2 can have multiple rows where LinkId are the same.
So what I have been trying to figure is a mysql query to Select all rows in Table1 that have a linked row or more in Table2 where Field3 contains a certain value. Is this easily possible?
Simply use JOIN
SELECT Table1.*
FROM Table1 A JOIN Table2 B ON A.Id = B.LinkID
WHERE B.Field3 IN ('Your inputs')
You can have multiple tables in the FROM clause:
SELECT *
FROM Table1, Table2
WHERE Table2.Field3 = 'certain value'
AND Table1.Id = Table2.LinkId

Join two tables based on a view, sql

I have the following sql situation
Table1
id name
Table2
id name
_Table1ToTable2
id, id_table1, id_table2
Result:
id, name, table2_name, table2_id
I would like to join Table1 and Table2 into a single query, but I cannot find a way to make a join between them, any ideas?
It seems like you want to use bridge table Table1ToTable2 to join Table1 and Table2. This can be done simply with 2 INNER JOINs :
SELECT
tt.id AS tt_id,
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
Table1ToTable2 AS tt
INNER JOIN Table1 AS t1
ON t1.id = tt.id_table1
INNER JOIN Table2 AS t2
ON t2.id = tt.id_table2
if there is no relationship between table1 and table 2 then,
select table1.id, table1.name, table2.id, table2.name from
table1, table2
if table1 and table2 are related with ID then,
select table1.id, table1.name, table2.id, table2.name from
table1
inner join table2
on table1.id = table2.id
If your intention is to retrieve a result set with every possible combination of the id columns from both tables then you can do as follows:
select id = identity (int, 1, 1), * into #a from (select distinct table1. id as id1, table2. id as id2 from table1 cross apply table2) d; select * from #a; drop table #a
If you do not wish to use a temp table an alternative would be to use some variation of the row_number() function.
The above solves for an assumed requirement as your question lacks context. Elaborate on your requirements and we will be able to provide you with a more relevant and detailed answer.

SQL how to compare two columns from two different tables

I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns. I have two columns in table1 that I want to compare them with two columns in table2.
Table 1 have column1 and column2 (that needs to be compared)
Table 2 have column6 and column7 (that needs to be compared)
I need to compare the combination of the two columns. I tried to do the below query however it doesn't work
Select * from table1
where column1, column2 NOT IN (Select column6, column7 from table2)
How can I compare the two columns in the the two tables?
Except shows the difference between two tables (the Oracle guys use minus instead of except and the syntax and use is the same). It is used to compare the differences between two tables. For example, let's see the differences between the two tables
SELECT * FROM
table1
EXCEPT
SELECT * FROM
table2
Try a minus statement. This will give you any results from the first select statement of table1 that aren't in the second select statement on table2.
select column1, column2 from table1
minus
select column6, column7 from table2
NOT EXISTS is a "null safe" version of NOT IN.
If you mean the combination column1 AND column2 not in same row in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6
and table1.column2 = table2.column7)
Or if you mean just column1 and column2 values can't even be in different rows in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6)
and NOT EXISTS (select 1 from table2
where table1.column2 = table2.column7)
The query with the least comparisions I can think of is
Select t1.*
from table1 t1
left join table2 t2 on t1.column1 in (t2.column6, t2.column7)
or t1.column2 in (t2.column6, t2.column7)
where t2.column6 is null
SELECT * FROM table1 t1
RIGHT JOIN table2 t2
WHERE
t1.c1 = t2.c6 AND
t1.c2 = t2.c7
Please try this query:
Select
case when (table1.column1 = table2.column6)
then 1 else 0
end column1_6 check,
case when (table1.column2 = table2.column7)
then 1 else 0
end
from
table1
inner join
table2 on table1.ID = Table2.ID
select * from table1 where column1 not in(select column 6 from table2) or column2 not in(select column7 from table2)
This will give you rows from table1 where there are differences between col1 and col6 or col2 and col7
Hope this helps

PDO Query on joined tables when column names are the same

I'm running a query via PDO on a pair of joined tables like so:
SELECT table1.id, table2.id, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;
Both tables have an id column so when I run fetchAll() the associative array only contains one id field. This is because the first is overwritten by the second.
Is there a way to obtain both id fields? Perhaps by having the table name included in the array keys...
use aliases
SELECT table1.id as t1id, table2.id as t2id
--etc.
try this
SELECT table1.id AS idtable1, table2.id AS idtable2, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;