I am in need of generating a result which joins two user related tables - Users and Usermeta. These are from a Wordpress MySQL DB.
User Table -
ID user_email
1 abc#gmail.com
2 xyz#gmail.com
3 1sf#email.com
Usermeta Table -
Umeta_ID user_id meta_key meta_value
1000 1 billing_phone 9876443100
1001 1 billing_postcode 670001
1002 1 billing_address Somewhere here, Delhi
1003 2 billing_phone 9876345188
1004 2 billing_postcode 650021
1005 2 billing_address 7th Lane, Bangalore
1003 3 billing_phone 7852562100
1004 3 billing_postcode 400521
1005 3 billing_address Fancy Area, Mumbai
As you can the user_id column in Usermeta table refers the ID column from the User table.
I am in dire need of a MYSQL query which gives out the result in this manner -
ID user_email billing_phone billing_postcode billing_address
1 abc#gmail.com 9876443100 670001 Somewhere here, Delhi
2 abc#gmail.com 9876345188 650021 7th Lane, Bangalore
3 1sf#gmail.com 7852562100 400521 Fancy Area, Mumbai
I tried doing an UNION on two joins like this
SELECT 6ft_users.ID, 6ft_usermeta.meta_value as PHONE_NUMBER
FROM 6ft_users
INNER JOIN 6ft_usermeta
ON 6ft_users.ID=6ft_usermeta.user_id
WHERE 6ft_usermeta.meta_key LIKE 'billing_phone'
UNION
SELECT 6ft_users.ID, 6ft_usermeta.meta_value as POSTCODE
FROM 6ft_users
INNER JOIN 6ft_usermeta
ON 6ft_users.ID=6ft_usermeta.user_id
WHERE 6ft_usermeta.meta_key LIKE 'billing_postcode';
But this is not helping as I get only one column - PHONE_NUMBER. I tried a few other things but to no avail. Please help me out.
Thanks in advance!
Try this:
SELECT U.ID,
U.user_email,
MAX(IF(UM.meta_key = 'billing_phone', UM.meta_value, '')) AS billing_phone,
MAX(IF(UM.meta_key = 'billing_postcode', UM.meta_value, '')) AS billing_postcode,
MAX(IF(UM.meta_key = 'billing_address', UM.meta_value, '')) AS billing_address
FROM 6ft_users U
LEFT JOIN 6ft_usermeta UM ON U.ID = UM.user_id
GROUP BY U.ID
Related
I have 3 Tables, new to SQL, I'm Trying to Combine 2 tables (Users and Tasks) into Companies or into a new table. What I need is to probably replace under Companies Table: TaskID and UserID with TaskID and UserID in both Users Tasks and table, but I get an error
SELECT COMPANY.UserID, COMPANY.TaskID, (FirstName+' '+LastName) AS FullName, TASKS.TaskSubject, USERS.UserID
FROM USERS, TASKS, COMPANY
INNER JOIN COMPANY.UserID = USERS.UserID
INNER JOIN COMPANY.TaskID = TASKS.TaskSubject
USERS:
UserID FirstName LastName
1 John Green
2 Graham Dale-Jones
3 Francois Peters
4 Danika Snow
5 Jennifer Booth
6 Erin Harvey
7 Caleb Jackson
TASKS:
TaskID TaskSubject TaskManager
101 Install
102 Upgrade
103 Troubleshoot
104 Assign
COMPANY:
CompanyID TasksID UserID
1 101 1
1 101 2
2 102 2
3 103 3
4 103 4
5 104 7
Thank you for the help
Try the following, you first need to learn how to join. What you are using is implicit join (on where) which is not best practice, instead you should always use explicit join.
SELECT
c.UserID,
c.TaskID,
concat(FirstName, ' ' ,LastName) AS FullName,
t.TaskSubject,
u.UserID
FROM users u
INNER JOIN company c
on c.UserID = u.UserID
INNER JOIN tasks t
on c.TaskID = t.TaskId
Here is the demo.
Trying to create a report across 3 tables - company, account, user. For each company, there's an ID in account. Each user has an account. I can get totals easily enough, but I need to add count of how many users out of the total are registered (username is not null).
SELECT c.c_name, c.c_groupnumber, count(a.a_userid) AS TotalCount
FROM company c
LEFT JOIN account a ON c.c_groupnumber = a.a_groupnumber
WHERE a.a_deleted IS NULL
GROUP BY c.c_groupnumber
HAVING TotalCount > 0;
How can I add in a condition that gives me a count of user.u_username not null while maintaining my TotalCount? The link between account and user is
a.a_userid = user.u_userid
tbl.company
c_id, c_groupnumber, c_name
1 1234 widgets, inc.
2 5678 joe's garage
tbl.user
u_userid, u_username, u_name
1 bill Bill Smith
2 frank Frank Johnson
3 NULL Jane Doe
4 mary Mary Stack
5 NULL Steve Spot
tbl.account
a_id, a_userid, a_groupnumber
100 1 1234
101 2 5678
102 3 5678
103 4 1234
104 5 1234
So using the above very simplified table example, company "Widget's Inc." has 3 employees (bill smith, mary stack and steve spot), and of those 3 2 have registered (bill and mary), while steve has not (username is null).
Joe's Garage has 2 employees - Frank and Jane, and Frank has registered, while Jane has not.
I'd love to generate a report something like this:
Group Company Total Emp Reg Emp
1234 Widgets Inc 3 2
5678 Joe's Garag 2 1
Hopefully that makes the question clearer?
What if you get the count of username and then perform a JOIN with that like
SELECT c.c_name, c.c_groupnumber, count(a.a_userid) AS TotalCount,
xxx.username_count
FROM company c
LEFT JOIN account a ON c.c_groupnumber = a.a_groupnumber
LEFT JOIN ( select u_userid, count(u_username) username_count
from `user`
group by u_userid ) xxx ON a.a_userid = xxx.u_userid
WHERE a.a_deleted IS NULL
GROUP BY c.c_groupnumber
HAVING TotalCount > 0;
I have three tables "Users" , "Subjects" and "Marks" like
Users Table
id name
1 A
2 B
3 C
4 D
5 E
6 A
7 B
Subjects Table
id name
1 Chemistry
2 Physics
3 English
4 Maths
5 History
Marks Table
u_id is the foreign key of Users (id) and s_id is foreign key of Subjects(id)
id u_id s_id marks
1 1 1 60
2 1 2 70
3 1 3 80
4 2 2 80
5 2 3 44
6 3 1 50
7 5 4 50
8 4 5 50
9 5 4 100
10 2 5 100
and I wish for the result to be like
id Name Chemistry Physics English
1 A 60 70 80
2 B NULL 80 44
3 3 50 NULL NULL
Using Join
So far I have only been able to get
name name marks
A English 80
A Physics 70
A Chemistry 60
B English 44
B Physics 80
C Chemistry 50
Using the following query
SELECT u.name, s.name , m.marks
FROM Users as u
RIGHT JOIN Marks as m ON m.u_id = u.id
LEFT JOIN Subjects as s ON m.s_id = s.id
WHERE s.name = "English"
OR s.name = "Physics"
OR s.name = "Chemistry"
ORDER BY u.name; "
Well, after reading the answers, I wanted to post my own one:
SELECT
u.id
, u.name
, MAX(IF(s.id = 1, COALESCE(m.mark), 0)) as 'Chem'
, MAX(IF(s.id = 2, COALESCE(m.mark), 0)) as 'Phys'
, MAX(IF(s.id = 3, COALESCE(m.mark), 0)) as 'Eng'
FROM marks m
INNER JOIN subjects s
ON s.id = m.subjects_id
INNER JOIN users u
ON u.id = m.users_id
GROUP BY u.id
You can check that makes all you want in SqlFiddle: http://sqlfiddle.com/#!9/f567b/1
The important part is the grouping of all the elements according to the user id, and the way of writing the results from rows in a table to columns in another table. As written in #TheShalit answer, the way of achieving that is just assigning the value as a column. Problem is that when grouping by user, you'll have a lot of values there from where you have to select the important one (the one that is not 0 neither NULL, XD). COALESCE function makes sure that you always return a integer, just in case a NULL is given.
It's also important to notice that you'll have to build the SQL with the names of the subjects and the ids from database, as SQL can't retrieve the name of the elements to write them directly as names of the columns. That's why I wrote 'Chem', 'Phys' and 'Eng' instead of the right names. In fact, would be easier if you just wrote the id of the subject instead of a name, just to retrieve the elements later when you'll fetch the rows.
Take into account that is VERY IMPORTANT that you'll table will have the right indexes there. Make sure you have an UNIQUE id on the table marks with users and subjects to avoid having more than one value there stored
Use select like this(with joins and group by student):
MAX(If(subjects.name="Chemistry",marks.marks,'')) Chemistry,
MAX(If(subjects.name="Physics",marks.marks,'')) Physics,
.....
You will need to do something like:
SELECT u.NAME AS NAME,
m_e.marks AS english,
m_p.marks AS physics,
m_c.marks AS chemistry
FROM users AS u
JOIN marks AS m_e ON m_e.u_id = u.id
JOIN marks AS m_p ON m_p.u_id = u.id
JOIN marks AS m_c ON m_c.u_id = u.id
WHERE m_e.s_id = 3 AND m_c.s_id = 1 AND m_p.s_id = 2
You are getting 3 different values from a single table but different rows so you need to join the marks table with itself to be able to get the values from 3 different records into 1 result row
I used the values that you defined as primary id's for your 3 subjects in your question in the where clause to make sure you are getting the correct result for each subject
i have these tables:
tb_employee:
ID EMPLOYEE
1 Jhonatan Sandoval
2 Patricia Sanchez
3 Ken Dawson
tb_bankacc:
ID BANK AMOUNT OWNER (from tb_employee)
1 Bank 1 250000 1
tb_pay:
ID OWNER EMPLOYEE AMOUNT
1 1 2 500
2 1 3 480
I need to make a SELECT QUERY to show the names of employees, like this:
ID OWNER EMPLOYEE AMOUNT
1 Jhonatan Sandoval Patricia Sanchez 500
2 Jhonatan Sandoval Ken Dawson 480
But, i don't know how.
Use
SELECT p.Id, o.Employee AS Owner, e.Employee, p.Amount
FROM tb_pay p
INNER JOIN tb_employee e ON e.Id = p.Employee
INNER JOIN tb_employee o ON o.Id = p.Owner
You're joining to the tb_employee table twice, once to get the names for the Employee column (by joining the Id in the Employee column with the Id in tb_employee - and then taking the name from that record) and then again to get the names for the Owner column (joining the Id in the Owner column to the Id in tb_employee).
I have this table
user_id time_completed
4 86.30887
5 57.81364
5 35.50281
5 10.00000
5 74.19355
5 31.91489
6 15.00000
6 20.50000
I need to sum all the time for each user, something like this:
user_id time_completed
4 86.30887
5 209.42489
6 35.50000
This is how I get the first table:
$result = mysql_query ("SELECT user_id,time_completed FROM `mytable` ORDER BY `mytable`.`user_id` ASC;" )
Any idea?
EDIT:
What if I need to replace user_id for the name in the following table (db_users)?
id username
1 admin
2 peter
3 tom
4 user
5 joey
6 helen
EDIT2:
I've modified this table (db_users) and I want country also appears in the query.
id username country
1 admin ES
2 peter IT
3 tom US
4 user GB
5 joey GE
6 helen FR
Like this:
user_id time_completed country
4 86.30887 GB
5 209.42489 GE
6 35.50000 FR
Take a look here: http://sqlfiddle.com/#!2/24d1b/11
you need to use SUM() which is an aggregate function and group them by their user_id
SELECT user_ID, SUM(time_COmpleted) totalTime
FROM tableName
GROUP BY user_ID
SQLFiddle Demo
UPDATE 1
SELECT b.username, SUM(time_COmpleted) totalTime
FROM tableName a
INNER JOIN db_users b
ON a.user_id = b.id
GROUP BY b.username
SQLFiddle Demo