I have two mysql tables: table_1 and table_2.
table_1:
| c_id | name | email |
| 1 | tom | t#t.com |
table_2:
| a_id | c_id | address | street |
| 1 | 1 | 67 | home |
| 2 | 1 | 68 | main |
How to create mysql query that will select table_1.name, table_1.email, table_2.address and table_2.street returning only one record like:
| name | email | address | street |
| tom | t#t.com | 67 | home |
Thanks for any suggestion.
Regards, T
Try this:
SELECT table_1.name, table_1.email, table_2.address, table_2.street
FROM table_1
JOIN table_2
ON table_1.c_id = table_2.c_id
GROUP BY table_1.c_id
SQLfiddle demo
The GROUP BY will determine which column you want to use to group the results by. B.T.W. if you like the change the column head you can add AS followed by the column head name (table_1.name AS "First name").
Table structure and sample data:
CREATE TABLE table_1
(`c_id` int, `name` varchar(3), `email` varchar(7))
;
INSERT INTO table_1
(`c_id`, `name`, `email`)
VALUES
(1, 'tom', 't#t.com')
;
CREATE TABLE table_2
(`a_id` int, `c_id` int, `address` int, `street` varchar(4))
;
INSERT INTO table_2
(`a_id`, `c_id`, `address`, `street`)
VALUES
(1, 1, 67, 'home'),
(2, 1, 68, 'main')
;
If you like to limit the sql query to a certain person say tom WHERE table_1.name LIKE 'Tom'. Like so:
SELECT table_1.name, table_1.email, table_2.address, table_2.street
FROM table_1
JOIN table_2
ON table_1.c_id = table_2.c_id
WHERE table_1.name LIKE 'Tom'
GROUP BY table_1.c_id;
You can also use = but with LIKE you can use wildcards like T%
you should use the SQL statement LEFT JOIN, for example
SELECT name, email, address, street
FROM table1
LEFT JOIN table2
ON table1.c_id=table2.c_id;
with the possible option WHERE name = 'tom'
select table_1.name as name, table_1.email as email, table_2.address as address, table_2.street as street
from table_1
left join table_2 on table_1.c_id = table_2.c_id
Related
Below are two mysql tables. I want to check if the two people are in teams from Table: Teams and then getting their name and email from Table: Registration. I am having a hard time writing a mysql query for it.
Table: Registration
id: 1 name:jason email:jason#xyz.com
id: 2 name:kim email:kim#xyz.com
id: 3 name:tim email:tim#xyz.com
Table:Teams
team_id: 1 person1Id: 1 person2Id: 2
team_id: 2 person1Id: 1 person2Id: 3
You have to join the registration table twice and give them a unique alias
Schema (MySQL v8.0)
CREATE TABLE Teams (
`team_id` INTEGER,
`person1Id` VARCHAR(9),
`person2Id` INTEGER
);
INSERT INTO Teams
(`team_id`, `person1Id`, `person2Id`)
VALUES
('1', '1', '2'),
('1', '1', '3');
CREATE TABLE Registration (
`id` INTEGER,
`name` VARCHAR(5),
`email` VARCHAR(13)
);
INSERT INTO Registration
(`id`, `name`, `email`)
VALUES
('1', 'jason', 'jason#xyz.com'),
('2', 'kim', 'kim#xyz.com'),
('3','tim','tim#xyz.com');
Query #1
SELECT
t.team_id,r1.name,r1.email,r2.name,r2.email
FROM Teams t
INNER JOIN Registration r1 ON t.person1Id = r1.id
INNER JOIN Registration r2 ON t.person2Id = r2.id;
| team_id | name | email | name | email |
| ------- | ----- | ------------- | ---- | ----------- |
| 1 | jason | jason#xyz.com | kim | kim#xyz.com |
| 1 | jason | jason#xyz.com | tim | tim#xyz.com |
View on DB Fiddle
you can select both ids from Teams table and use in clause to get name and email from Registration table as
select name, email from Registration where id in(select person1Id, person2Id from Teams where team_id=1)
I have a table like the below:
Site | Name | ID
A | Mike | 1
A | Mary | 2
A | Mary | 3
B | Mary | 1
B | Rich | 2
I'd like to find all the duplicate Name's within a Site. So I'm trying to return:
Site | Name | ID
A | Mary | 2
A | Mary | 3
I've tried this:
SELECT DISTINCT Site, Name, ID
from table
group by ID having count(*) > 1
The results come back erroneously because it's counting Sites A & B together. I would like to only find the duplicates for within each Site--not duplicates across Sites.
You can use exists or in:
select t.*
from t
where exists (select 1
from t t2
where t2.site = t.site and t2.name = t.name and t2.id <> t.id
);
You can try to use NOT exists with subquery having.
the duplicate Name's within a Site
CREATE TABLE T(
Site varchar(5),
Name varchar(5),
ID int
);
insert into t values ('A','Mike', 1);
insert into t values ('A','Mary', 2);
insert into t values ('A','Mary', 3);
insert into t values ('B','Mary', 1);
insert into t values ('B','Rich', 2);
Query 1:
SELECT * FROM T t1 WHERE
NOT exists
(
SELECT 1
FROM T tt
where t1.name = tt.name
group by Name
HAVING MIN(tt.ID) = t1.ID
)
Results:
| Site | Name | ID |
|------|------|----|
| A | Mary | 2 |
| A | Mary | 3 |
find the duplicates for within each Site
CREATE TABLE T(
Site varchar(5),
Name varchar(5),
ID int
);
insert into t values ('A','Mike', 1);
insert into t values ('A','Mary', 2);
insert into t values ('A','Mary', 3);
insert into t values ('B','Mary', 1);
insert into t values ('B','Rich', 2);
Query 1:
SELECT t1.*
FROM T t1
WHERE not exists
(
SELECT 1
FROM T tt
where t1.name = tt.name and t1.Site = tt.Site
group by Name,Site
HAVING MIN(tt.ID) = t1.ID
)
Results:
| Site | Name | ID |
|------|------|----|
| A | Mary | 3 |
I'm struggling with 1 excercise since month I guess and hope you can help me. I've tried subquery in from, with or join tables in many ways and didn't received proper result.
OK. One row in Table1 stores information like:
Client's ID
firsname
lastame
Waiter id
One row in Table2 stores information like:
userID (client and waiter are the user)
firstname
lastname
I'm new to SQL but how I see this is:
WITH waiter AS(
SELECT
waiter_id,
firstname,
lastname
FROM Table1
JOIN Table2 ON Table1.waiter_id = Table2.user_id
)
SELECT
client_id,
firstname as Firstname_client,
lastname as Lastname_client,
waiter_id,
waiter.firstname as Firstname_waiter,
waiter.lastname as Lastname_waiter
FROM Table1
JOIN waiter ON Table1.waiter_id = waiter.waiter.id
I would be very grateful for any clues!
You can try this query. just use an alias name and JOIN
TestDLL
CREATE TABLE Table1(
client_id int,
firstname varchar(50),
lastname varchar(50),
waiter_id int
);
INSERT INTO Table1 values (1,'Andrew','x',5);
INSERT INTO Table1 values (2,'Chris','xx',6);
INSERT INTO Table1 values (3,'Anna','xxx',7);
INSERT INTO Table1 values (4,'Julia','xxxx',8);
CREATE TABLE Table2(
user_id int,
firstname varchar(50),
lastname varchar(50)
);
INSERT INTO Table2 values (1,'Andrew','x');
INSERT INTO Table2 values (2,'Chris','xx');
INSERT INTO Table2 values (3,'Anna','xxx');
INSERT INTO Table2 values (4,'Julia','xxxx');
INSERT INTO Table2 values (5,'Mat','xxxxx');
INSERT INTO Table2 values (6,'Kathy','xxxxxx');
INSERT INTO Table2 values (7,'Pual','xxxxxxx');
INSERT INTO Table2 values (8,'John','xxxxxxxx');
Query
SELECT t1.client_id ,
t1.firstname AS Firstance_client,
t1.lastname as Lastname_Client,
t1.waiter_id,
t2.firstname as Firstname_waiter ,
t2.lastname as Lastname_Waiter
FROM Table1 t1
inner join Table2 t2 on t1.waiter_id = t2.user_id
sqlfiddle
[Results]:
| client_id | Firstance_client | Lastname_Client | waiter_id | Firstname_waiter | Lastname_Waiter |
|-----------|------------------|-----------------|-----------|------------------|-----------------|
| 1 | Andrew | x | 5 | Mat | xxxxx |
| 2 | Chris | xx | 6 | Kathy | xxxxxx |
| 3 | Anna | xxx | 7 | Pual | xxxxxxx |
| 4 | Julia | xxxx | 8 | John | xxxxxxxx |
You can use below query to get third table
Select ClientId,
c.FirstName as Client_FirstName,
c.LastName Client_LastName,
w.FirstName as Waiter_FirstName,
w.LastName as Waiter_LastName,
w.UserId as WaiterId
FROm dbo.Table1 as c
LEFT JOIN dbo.Waiter as[enter image description here][1] w on c.WaiterId = w.UserId
Sql Snippet
I would like you to help me with this, I have a little problem with this, but I don't know how I can fix it, basically I want to update the so called duplicatedusernameid, and update it into the users database. Here is my code:
UPDATE users SET duplicatedusernameid = (SELECT CONCAT(first_name,middle_name,last_name)
,COUNT(*)-1 AS duplicatedusernameid HAVING COUNT(*) > 1) WHERE id = 1
And here is the working SELECT function, but instead of select I want UPDATE, like the one above, but that one doesn't work. Here is the SELECT code:
SELECT CONCAT(first_name,middle_name,last_name),COUNT(*)-1 AS duplicatedusernameid
FROM users HAVING COUNT(*) > 1
So basically I want to get the value of the duplicatedusernameid and update it.
You can use a SQL statement like this:
update users a
inner join (
-- get the count for a user
SELECT first_name, middle_name, last_name, COUNT(*)-1 AS duplicatedusernameid
from users
group by first_name, middle_name, last_name
) b
on a.first_name = b.first_name
and a.middle_name = b.middle_name
and a.last_name = b.last_name
set a.duplicatedusernameid = b.duplicatedusernameid
where a.id = 1;
Table
create table users (
id int,
first_name varchar(100),
middle_name varchar(100),
last_name varchar(100),
duplicatedusernameid varchar(100)
);
insert into users (id, first_name, middle_name, last_name) values
(1, 'John', '', 'Smith'),
(2, 'John', '', 'Smith'),
(3, 'Jessie', '', 'Marcus'),
(4, 'Jessie', '', 'Marcus'),
(5, 'Jessie', '', 'Marcus'),
(6, 'Don', '', 'Kassieth');
Results (if you remove the where clause)
| id | first_name | middle_name | last_name | duplicatedusernameid |
|----|------------|-------------|-----------|----------------------|
| 1 | John | | Smith | 1 |
| 2 | John | | Smith | 1 |
| 3 | Jessie | | Marcus | 2 |
| 4 | Jessie | | Marcus | 2 |
| 5 | Jessie | | Marcus | 2 |
| 6 | Don | | Kassieth | 0 |
Explanation
SELECT first_name, middle_name, last_name, COUNT(*)-1 AS duplicatedusernameid
from users
group by first_name, middle_name, last_name
The above SQL statement will get the count for each unique first, middle and last name combination. You can shortlist this by adding having count(*) > 1 below the group by clause.
We then join users table with this count-related table based on first, middle and last name combination. Wherever there is a match, update the retrieved count in the users table.
SQLFiddle
http://sqlfiddle.com/#!9/c43ea/1
I want to get a set of records where only the date (YYYY-MM-DD) and the username pair match in multiple records, all the other fields can be different.
SELECT *, count(*) from table
GROUP BY username
HAVING count(username)>1 AND count(date)>1;
Appears to be severely wrong, and I'm not sure how to make it work.
Most of the help I see here is about entire rows that match, I am only concerned about records that match on these two columns. I want to flag these duplicates.
I have no control over this database, so I can't take future maintenance suggestions.
create table table1
(
id int auto_increment primary key,
username varchar(30) not null,
`date` date not null
);
insert table1 (username,`date`) values ('john','2015-01-01');
insert table1 (username,`date`) values ('kim','2015-01-01');
insert table1 (username,`date`) values ('john','2015-01-01');
insert table1 (username,`date`) values ('john','2015-02-01');
insert table1 (username,`date`) values ('john','2015-03-01');
SELECT t1.*
from table1 t1
join
(
select username,`date`,count(*)
from table1
group by username,`date`
having count(username)>1
) inr
on inr.username=t1.username and inr.`date`=t1.`date`
results in 2 rows shown
+----+----------+------------+
| id | username | date |
+----+----------+------------+
| 1 | john | 2015-01-01 |
| 3 | john | 2015-01-01 |
+----+----------+------------+
2 rows in set (0.03 sec)
Edit:
as per OP request, have a column to flag dupes for later work, as opposed to a select statement. Note you can Alter Table and add this nullable flag column, set it, use values at your leisure, later Alter Table and drop it.
But I will just start over here with the create table with new flag column:
create table table1
(
id int auto_increment primary key,
username varchar(30) not null,
`date` date not null,
dupeflag int null -- <---- New flag column, nullable, ignored on inserts below
);
insert table1 (username,`date`) values ('john','2015-01-01');
insert table1 (username,`date`) values ('kim','2015-01-01');
insert table1 (username,`date`) values ('john','2015-01-01');
insert table1 (username,`date`) values ('john','2015-02-01');
insert table1 (username,`date`) values ('john','2015-03-01');
update table1 t1
join
( select username,`date`,count(*)
from table1
group by username,`date`
having count(username)>1
) inr
on inr.username=t1.username and inr.`date`=t1.`date`
set dupeflag=1;
-- 2 rows affected
select * from table1;
+----+----------+------------+----------+
| id | username | date | dupeflag |
+----+----------+------------+----------+
| 1 | john | 2015-01-01 | 1 |
| 2 | kim | 2015-01-01 | NULL |
| 3 | john | 2015-01-01 | 1 |
| 4 | john | 2015-02-01 | NULL |
| 5 | john | 2015-03-01 | NULL |
+----+----------+------------+----------+
5 rows in set (0.00 sec)