How to link these two tables in sql
up vote
1
down vote
favorite
I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
For example:A user is logged In and want to add others.
If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.
Will become clear from the following scenario.
I'v two table one is user
user, and the second is userdata
userdata.
Column id
is primary key in user
and Foreign key in userdata
as adder
.
I want to insert the data into the table called user
when the structure is something like this.
user
id name
1 Alice
2 Bob
3 jhon
userdata
adder added
1 2
1 3
the userdata
table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata
like
1 2 i.e (**Alice added Bob**)
and when there is second row populated from front end that says
2 1 i.e (**Bob added Alice**)
so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.
php mysql
add a comment |
up vote
1
down vote
favorite
I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
For example:A user is logged In and want to add others.
If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.
Will become clear from the following scenario.
I'v two table one is user
user, and the second is userdata
userdata.
Column id
is primary key in user
and Foreign key in userdata
as adder
.
I want to insert the data into the table called user
when the structure is something like this.
user
id name
1 Alice
2 Bob
3 jhon
userdata
adder added
1 2
1 3
the userdata
table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata
like
1 2 i.e (**Alice added Bob**)
and when there is second row populated from front end that says
2 1 i.e (**Bob added Alice**)
so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.
php mysql
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
For example:A user is logged In and want to add others.
If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.
Will become clear from the following scenario.
I'v two table one is user
user, and the second is userdata
userdata.
Column id
is primary key in user
and Foreign key in userdata
as adder
.
I want to insert the data into the table called user
when the structure is something like this.
user
id name
1 Alice
2 Bob
3 jhon
userdata
adder added
1 2
1 3
the userdata
table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata
like
1 2 i.e (**Alice added Bob**)
and when there is second row populated from front end that says
2 1 i.e (**Bob added Alice**)
so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.
php mysql
I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
For example:A user is logged In and want to add others.
If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.
Will become clear from the following scenario.
I'v two table one is user
user, and the second is userdata
userdata.
Column id
is primary key in user
and Foreign key in userdata
as adder
.
I want to insert the data into the table called user
when the structure is something like this.
user
id name
1 Alice
2 Bob
3 jhon
userdata
adder added
1 2
1 3
the userdata
table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata
like
1 2 i.e (**Alice added Bob**)
and when there is second row populated from front end that says
2 1 i.e (**Bob added Alice**)
so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.
php mysql
php mysql
asked Nov 10 at 15:21
Khan
135
135
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
A JOIN is what you are looking for.
If you want to know who added who, you can use this query
Schema (MySQL v5.7)
CREATE TABLE users
(
id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE userdata
(
adder INT(6) NOT NULL,
added INT(6) NOT NULL,
CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
PRIMARY KEY (adder, added)
);
INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");
INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);
Query #1
SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
FROM users aer
INNER JOIN userdata ud ON aer.id = ud.adder
INNER JOIN users aed ON aed.id = ud.added
WHERE
(
SELECT COUNT(*)
FROM userdata uda
WHERE uda.adder = ud.added
AND uda.added = ud.adder
) >= 1;
Outputs :
| Adder | Action | Added |
| ----- | ------ | ----- |
| Bob | added | Alice |
| Alice | added | Bob |
View on DB Fiddle
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
|
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
A JOIN is what you are looking for.
If you want to know who added who, you can use this query
Schema (MySQL v5.7)
CREATE TABLE users
(
id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE userdata
(
adder INT(6) NOT NULL,
added INT(6) NOT NULL,
CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
PRIMARY KEY (adder, added)
);
INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");
INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);
Query #1
SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
FROM users aer
INNER JOIN userdata ud ON aer.id = ud.adder
INNER JOIN users aed ON aed.id = ud.added
WHERE
(
SELECT COUNT(*)
FROM userdata uda
WHERE uda.adder = ud.added
AND uda.added = ud.adder
) >= 1;
Outputs :
| Adder | Action | Added |
| ----- | ------ | ----- |
| Bob | added | Alice |
| Alice | added | Bob |
View on DB Fiddle
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
|
show 4 more comments
up vote
0
down vote
accepted
A JOIN is what you are looking for.
If you want to know who added who, you can use this query
Schema (MySQL v5.7)
CREATE TABLE users
(
id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE userdata
(
adder INT(6) NOT NULL,
added INT(6) NOT NULL,
CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
PRIMARY KEY (adder, added)
);
INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");
INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);
Query #1
SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
FROM users aer
INNER JOIN userdata ud ON aer.id = ud.adder
INNER JOIN users aed ON aed.id = ud.added
WHERE
(
SELECT COUNT(*)
FROM userdata uda
WHERE uda.adder = ud.added
AND uda.added = ud.adder
) >= 1;
Outputs :
| Adder | Action | Added |
| ----- | ------ | ----- |
| Bob | added | Alice |
| Alice | added | Bob |
View on DB Fiddle
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
|
show 4 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
A JOIN is what you are looking for.
If you want to know who added who, you can use this query
Schema (MySQL v5.7)
CREATE TABLE users
(
id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE userdata
(
adder INT(6) NOT NULL,
added INT(6) NOT NULL,
CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
PRIMARY KEY (adder, added)
);
INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");
INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);
Query #1
SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
FROM users aer
INNER JOIN userdata ud ON aer.id = ud.adder
INNER JOIN users aed ON aed.id = ud.added
WHERE
(
SELECT COUNT(*)
FROM userdata uda
WHERE uda.adder = ud.added
AND uda.added = ud.adder
) >= 1;
Outputs :
| Adder | Action | Added |
| ----- | ------ | ----- |
| Bob | added | Alice |
| Alice | added | Bob |
View on DB Fiddle
A JOIN is what you are looking for.
If you want to know who added who, you can use this query
Schema (MySQL v5.7)
CREATE TABLE users
(
id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE userdata
(
adder INT(6) NOT NULL,
added INT(6) NOT NULL,
CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
PRIMARY KEY (adder, added)
);
INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");
INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);
Query #1
SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
FROM users aer
INNER JOIN userdata ud ON aer.id = ud.adder
INNER JOIN users aed ON aed.id = ud.added
WHERE
(
SELECT COUNT(*)
FROM userdata uda
WHERE uda.adder = ud.added
AND uda.added = ud.adder
) >= 1;
Outputs :
| Adder | Action | Added |
| ----- | ------ | ----- |
| Bob | added | Alice |
| Alice | added | Bob |
View on DB Fiddle
edited Nov 10 at 15:42
answered Nov 10 at 15:26
Cid
2,58811022
2,58811022
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
|
show 4 more comments
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
– Khan
Nov 10 at 15:33
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
– Khan
Nov 10 at 15:37
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
Oh I see, you might need a subquery then. Editing.
– Cid
Nov 10 at 15:38
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
@Khan : I updated my answer
– Cid
Nov 10 at 15:42
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
– Khan
Nov 10 at 15:50
|
show 4 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240365%2fhow-to-link-these-two-tables-in-sql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown