Mysql join two table - mysql

I have two table in two different database.
Kalix2 -->I have one table ph_Companies
Asterisk--> Here I have table called cdr.
In ph_Companies I am having CompanyName column and company name is in this format.
CompanyName
AccuWebHosting
In other table I am having another column clid which stores the company name in the following format.
clid
"AccuWebHosting-EN" <6618083563>
Now, my question is how can I make join between these two table based on this column. In both the table these are the only column which is having some common data. No other column is having similar data.
Please help me. Thanks in advance.

SELECT a.*, b.*
FROM ph_Companies a
INNER JOIN otherTable b
ON b.clid LIKE CONCAT('%', a.CompanyName, '%')
Caution, this query is very slow if you want large database because it requires a FullTable Scan. The reason is you have "bad" schema design. I'll recommend you to alter the tables like this,
CREATE TABLE ph_Companies
(
ID INT PRIMARY KEY,
CompanyName VARCHAR(20),
-- OTHER COLUMNS HERE
);
CREATE TABLE otherTable
(
ID INT AUTO_INCREMENT PRIMARY KEY,
CompanyID INT,
CompanyName VARCHAR(20),
clid VARCHAR(50),
-- OTHER COLUMNS HERE
CONSTRAINT tb_fk FOREIGN KEY (CompanyID)
REFERENCES ph_Companies (ID)
);
and you will have a query like this,
SELECT a.*, b.*
FROM ph_Companies a
INNER JOIN otherTable b
ON a.ID = b.CompanyID
which is FAR more better than the query above.

Related

How to get all the values of ID in two tables without using set operations

Two tables
Customer_Fixed_Deposit(
ID int primary key,
name varchar(20),
Fixed_Deposit int
);
Customer_Loan(
ID int primary key,
name varchar(20),
Loan int
);
I want the ID of all the customers and with names in both the tables without using set operations
I tried to insert all the values of id and name in 1 table into another, because there were duplicates and ID is the primary key it did not work
You can get all customers with the same name, assuming names are unique.
SELECT fd.id, fd.name, l.id, l.name
FROM Customer_Fixed_Deposit fd
JOIN Customer_Loan l
ON fd.name = l.name;
Now, if you want to insert only those customers to Customer_Loan table who are not already present in it.
INSERT INTO Customer_Loan (name)
SELECT name
FROM Customer_Fixed_Deposit
WHERE NOT EXISTS (
SELECT fd.id
FROM Customer_Fixed_Deposit fd
JOIN Customer_Loan l
ON fd.name = l.name;
);
If I understand you correctly, you have two tables with two different sets of people and IDs? If so, then I think a UNION would allow you to select all the IDs and Names from each table into one result. Try this:
SELECT
ID, name
FROM
Customer_Fixed_Deposit
UNION
SELECT
ID, name
FROM
Customer_Loan;

Displaying top-selling product pairs

I have a problem, I've spent a lot of time on. I have to display the pairs of items (Item1, Item2, How many) that appear on the invoices - and how many pairs have repeated. The table that connects invoices to goods is called "Transactions".
I Have 3 tables:
Items: IdItem, Name
Transactions: IdTransaction, IdInvoice, IdItem
Invoices: IdInvoice
Result of SQL Query must be:
Item1 | Item2 | Number
This sounds like a self-join and aggregation:
select t1.IdItem as IdItem1, t2.IdItem as IdItem2, count(*)
from transactions t1 join
transactions t2
on t1.IdInvoice = t2.IdInvoice and
t1.IdItem < t2.IdItem
group by t1.IdItem, t2.IdItem
order by count(*) desc;
Assuming your tables look like this:
CREATE TABLE Items (
IDItem INT,
Name VARCHAR,
PRIMARY KEY(IDItem)
);
CREATE TABLE Invoices (
IDInvoice INT,
PRIMARY KEY(IDInvoice)
);
CREATE TABLE Transactions (
IDTransaction INT,
IDInvoice INT,
IDItem INT,
PRIMARY KEY(IDTransaction),
FOREIGN KEY (IDItem) REFERENCES Items(IDItem),
FOREIGN KEY (IDInvoices) REFERENCES Invoices(IDInvoices)
)
If this is true then you need to query the results like this:
SELECT i.Name,in.IDInvoice FROM Transactions t
JOIN Items i ON i.IDItem = t.IDItem
JOIN Invoices in ON in.IDInvoice = t.IDInvoice
WHERE i.Name = item1

Using LIKE %% in query clause with multiple values from different tables

I have a basic query that fetches values from three tables. The three tables : company, classes_by_company, and person. I have a foreign key in all tables with the name company_id. In the where clause I am using LIKE to compare with a random value the values fetched for any results that may have a match. But I am having no success in doing so. I get an error in the sql line WHERE a.id LIKE %1% OR a.company_id LIKE %3% OR c.count LIKE %1% OR p.count LIKE %1%. How can I compare all the values fetched from these three tables against a random value to see if there is a match? SQLFIDDLE
SELECT a.id, a.company_id, a.status,
c.count AS classes_per_company,
p.count AS employees_per_company
FROM company a
LEFT JOIN (SELECT company_id, COUNT(*) as count
FROM classes_by_company
GROUP BY company_id) c
ON a.company_id = c.company_id
LEFT JOIN (SELECT company_id, COUNT(*) as count
FROM person
GROUP BY company_id) p
ON a.company_id = p.company_id
WHERE a.id LIKE %1% OR a.company_id LIKE %3% OR c.count LIKE %1% OR p.count LIKE %1%
Table Schema:
CREATE TABLE company
(
id int auto_increment primary key,
company_id int,
status varchar(20)
);
CREATE TABLE classes_by_company
(
id int auto_increment primary key,
company_id int,
class_name varchar(20)
);
CREATE TABLE person
(
id int auto_increment primary key,
employee_id int,
company_id int,
person_name varchar(20)
);
LIKE has to be followed by a string, so it should be:
WHERE a.id LIKE '%1%'
OR a.company_id LIKE '%3%'
OR c.count LIKE '%1%'
OR p.count LIKE '%1%'
id is an integer column; the LIKE operator relies on a text data type e.g. nvarchar.
You could convert the id to a string e.g. WHERE CAST(a.id AS VARCHAR(10)) LIKE '%1%' ...
Please also note the single quotes around the %1%.

Relational Tables - Select Only Rows in Table A That Don't Exist as ID in Table B

I have table A that stores courses and table B that stores current progress of users.
table A has unique ID column for each course (courseID) which also exists in table B with the same name (thus their relation).
I want to get all rows from table A whose IDs don't exist in table B. (e.g. no user has currently entered this course)
The two tables don't use foreign keys, just a column with the same name.
TRY
SELECT A.* FROM tableA A
LEFT JOIN tableB B ON USING (courseID)
WHERE B.courseID IS NULL
SELECT * FROM A WHERE A.courseID NOT IN (SELECT courseID FROM B);
You need LEFT JOIN
SELECT * FROM `tableA`
LEFT JOIN `tableB` ON `tableA`.`ID`=`tableB`.`ID`
WHERE `tableB`.`ID` IS NULL;
What database are you using?
If you're using Oracle, I think you want:
select A.student_code from A where not exists
( select 'x' from B where B.student_code = A.student_code)
At least that's what I think you were asking for..

Optimized SQL Query

Table Schema
For the two tables, the CREATE queries are given below:
Table1: (file_path_key, dir_path_key)
create table Table1(file_path_key varchar(500), dir_path_key
varchar(500), primary key(file_path_key)) engine = innodb;
Example, file_path_key = /home/playstation/a.txt
dir_path_key = /home/playstation/
Table2: (file_path_key, hash_key)
create table Table2(file_path_key varchar(500) not null, hash_key
bigint(20) not null, foreign key (file_path_key) references
Table1(file_path_key) on update cascade on delete cascade)
engine = innodb;
Objective:
Given a hash value *H* and a directory string *D*, I need to find all those
hashes which equal to *H* from Table2, such that, the corresponding file entry
doesn't have *D* as it's directory.
In this particular case, Table1 has around 40,000 entries and Table2 has 5,000,000 entries, which makes my current query really slow.
select distinct s1.file_path_key from Table1 as s1 join (select * from Table2 where hash_key = H) as s2 on s1.file_path_key = s2.file_path_key and s1.dir_path_key !=D;
The sub-select is really slowing your query down unnecessarily.
You should remove that and replace it with a simple join, moving pushing all of the non-join related criteria down into the WHERE clause.
Also you should add indexes on the Table1.dir_path_key and Table2.hash_key columns:
ALTER TABLE Table1
ADD INDEX dir_path_key dir_path_key(255);
ALTER TABLE Table2
ADD INDEX hash_key (hash_key);
Try something like this for the query:
select distinct s1.file_path_key
from Table1 as s1
join Table2 as s2 on s1.file_path_key = s2.file_path_key
where s1.dir_path_key !=D
and s2.hash_key =H;
I'd suggest selecting entries from Table2 into a temporary table first:
SELECT * FROM Table2 INTO #Temp WHERE hash_key = H
Then join the temporary table in your SELECT statement:
select distinct s1.file_path_key from Table1 as s1 join #Temp as s2 on s1.file_path_key = s2.file_path_key and s1.dir_path_key !=D;