I want to join 2 columns base on this :
A.Contactnumber and the 2 column B.old_contact, B.recent_contact
I want to retain the old contact and at the same time include recent if they're not alike
select a.*, b.old_contact, b.recent_contact
from table a
left join table b
on a.contactnumber = b.old_contact
SELECT a.contactnumber, b.old_contact, b.recent_contact
FROM a INNER JOIN b ON a.contactnumber=b.old_contact
WHERE b.old_contact!=b.recent_contact;
as the result you will get the values which are present in table "a" and in table "b" and which have different values in columns "old_contact" and "recent_contact" in table "b". Is that what you need?
Related
I have two tables as follows (in mysql):
Table: invoice
# Column Name
1 Id
2 invoice_date
3 invoice_no
4 consigned_to
5 invoiced_to
6 ...
Table: company
# Column Name
1 Id
2 title
3 ...
Both consigned_to and invoiced_to columns on first table are referencing company.Id.
What I am trying to achieve is a query with following columns
Column Name Table Name
Id (invoice)
invoice_date (invoice)
invoice_no (invoice)
consigned_to (invoice)
consigned_title (company.title)
invoiced_to (invoice)
invoiced_title (company.title)
I need unique column names for the consigned_title and invoiced_title columns, because I should be able to query those columns with titles from company table.
I managed to join single column like this with an alias:
SELECT invoice.*, company.title as consigned_title
from invoice
INNER JOIN company ON invoice.consigned_to = company.Id
but could not managed to reference the same column from company for joining with the invoice.invoiced_to. Is it even possible?
You need table aliases:
SELECT i.*, cc.title as consigned_title, ci.title as invoiced_title
FROM invoice i INNER JOIN
company cc
ON i.consigned_to = cc.Id INNER JOIN
company ci
ON i.invoiced_to = ci.id;
If you rename the id column from the company table for the two use cases then you avoid the need to use range variables (OK, so SQL still forces you to assign range variables -- I'm using c and i in this case -- but you don't need to use them, which is an important distinction):
SELECT invoice_no, consigned_title, invoiced_title
invoice
NATURAL JOIN
( SELECT id AS consigned_to, title AS consigned_title FROM company ) c
NATURAL JOIN
( SELECT id AS invoiced_to, title AS invoiced_title FROM company ) i;
I need to use mysql to show users (A) that not follow B, but they (A) are followed by B. This is gonna be used to suggest connections on a social media website.
My table structure:
ID
FOLLOWED
FOLLOWER
I've tried to use
LEFT JOIN
or
NOT EXISTS, but didn't work.
#DRapp shows me how to user LEFT JOIN on this query. That works. Now, how to add the users information on this query (LEFT JOIN + INNER JOIN).
Inner join need to be with follow table and user table (INNER JOIN users ON follow.follower = user.id)
Users`s table structure:
ID
Name
Given the scenario of sample data of "YourTable"
ID Follower Followed
1 A C
2 B C
3 B D
4 B E
5 C B
6 D A
The people followed by "B" include "C", "D" and "E" users.
From that, only user "C" follows "B". So, what you are looking for is any where no MATCH for A/B, B/A type of combination. So that is the basis of the LEFT-JOIN. The WHERE clause is only querying those records based on the user "B" you are considering as the originating "follower".
select
yt.Followed,
u.name
from
YourTable yt
left join YourTable yt2
on yt.Followed = yt2.Follower
AND yt.Follower = yt2.Followed
JOIN Users u
on yt.Followed = u.id
where
yt.Follower = 'B'
AND yt2.Followed IS NULL
To get then name, notice the join is to your ORIGINAL table that was based on user "B", but NOT finding the corrresponding inverse match. So, we are joining on the ORIGINAL record's Followed (who "B" is following) to the user table by the same ID in the user table. Then grab the user's name to the query.
There are two same structured tables i.e. One & Two. I want to update one column with values of another table's same column.
Have a look at this:
Table One
id name value
1 a 11
2 b 12
3 c 13
Table Two
id name value
1 c 11
2 d 12
3 e 13
I want to update one.name with the values of two.name. How do I do that?
Use a JOIN in the UPDATE to relate the two tables.
UPDATE One
JOIN Two ON One.value = Two.value
SET One.name = Two.name
If you need to use LIMIT, you have to use a subquery:
UPDATE One
JOIN (SELECT *
FROM Two
LIMIT 100) AS Two
ON One.value = Two.value
SET One.name = Two.name
In my info DB i have 4 tables, from which TWO are named as girls & boys and both have id column. boys contain 12 id(12 records) andgirls contain 8 id. when i apply this query:
SELECT * FROM
boys
NATURAL JOIN
girls
i get output as
Empty Set
How could this happen? (both id column is INT, NOT NULL, AUTO_INCREMENT).
NATURAL JOIN is shorthand for an INNER JOIN with equality predicates on columns of the same name.
For example, given two tables "m" and "f", which have columns named "id" and "name" in common, a NATURAL JOIN like this
SELECT ...
FROM m
NATURAL
JOIN f
Is equivalent to:
SELECT ...
FROM m
JOIN f
ON f.id = m.id
AND f.name = m.name
Only rows that "match" in the two tables will be returned in the result, all other rows are not returned.
For example, if we had table contents:
m: id name
-- ---------------
1 peter
2 paul
4 michael jackson
f: id name
-- ---------------
3 mary
4 michael jackson
The queries above would return only the rows from "m" and "f" that have id=4 (as a single row), because those are the only rows that "match".
Natural join will only work if the same named columns' values are same. ie if ID and Name both are same in both the tables.
If you need result like this :
Then you must name the 'name' col different in both the tables.
If you want a set of all boys and girls, you should use UNION.
select * from boys
union
select * from girls
Example :
Boys Table
Girls Table
Natural Join
Union Result
Natural Join The NATURAL [LEFT] JOIN of two tables is defined to be semantically equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables.
What this means is if you have any column name in common between the two tables, those are also being compared, and those rows are likely eliminated.
I have two tables sharing the same attribute 'attr'. The domain of values in attribute 'attr' in Table cust are a subset of domain of values in attribute 'attr' in table sales.
E.g Table cust containing 940, 8575, 454, 86869, 856869, 9686 as values in attribute 'attr', while Table sales contains 454, 86869, 856869, 8756, 5324, 946707, 9779. Also table cust contains 1 billion rows while Table sale contains 1 trillion rows. In order to perform the join I tried the following command:
select * from cust where cust.attr in(select distinct attr from table sales)
However, this turns out to be very slow..is there some efficient way to perform the join
1 billion rows !! ... 1 trillion rows !!
Create index on attr column in both the tables
ALTER TABLE cust ADD INDEX (attr);
ALTER TABLE sales ADD INDEX (attr);
Then do join using below syntax.
select * from cust c
inner join sales s
on c.attr=s.attr
SELECT cust.*
FROM cust
INNER JOIN sales attr
ON sales.attr = cust.attr
WHERE [query conditions here]
Make sure cust.attr and sales.attr are indexed, too
This post on JOINs will be helpful: http://webduos.com/mysql-join-syntax/