MySQL statement: spliting rows values into new columns - mysql

Using MySQL I need to return new first/last name columns for each user in table1.
**Table1**
uid
1
2
**Table2**
id fid uid field_value
1 1 1 Joe
2 2 1 Blow
3 1 2 Joe
4 2 2 Blogs
**Result**
uid first_name last_name
1 Joe Blow
2 Joe Blogs

The solution is simple,
select t1.uid, t21.field_value first_name, t22.field_value last_name
from table1 t1, table2 t21, table2 t22
where t1.uid=t21.uid and t1.uid=t22.uid and t21.fid=1 and t22.fid=2

This should do it (not tested):
select a.uid, a.field_value first_name, b.field_value last_name
from table2 a inner join table2 b on a.uid = b.uid
where a.fid = 1 and b.fid = 2

assuming you have columns first_name, and last_name in table2:
select uid, first_name, last_name from table1 left join table2 on table1.uid=table2.uid

Related

Getting values from 2 tables with different conditions for both tables

Table 1
ID
FirstName
LastNmae
city
Group
code
11
john
smith
abc
E
P
21
don
davis
def
E
P
3
vee
miller
ghi
Q
P
6
vee
miller
ghi
Q
P
Table 2
ID
FirstName
LastNmae
city
Status
EmpName
Phone
11
john
smith
abc
U
Company 1
123
21
don
davis
def
P
Company 2
456
3
vee
miller
ghi
C
Company 3
789
4
jim
jones
xyz
P
comapany4
001
I have 2 tables mentioned above. I need an output from both table under these conditions
For table 1 condition is:
Group='E' AND code='P'
For table 2 condition is : Status = 'U' OR Status = 'P'
For output required columns are:
ID, FirstName, LastName, City, EmpName, Phone
I cannot use UNION because number of columns mismatch.
Desired Output:
ID
FirstName
LastNmae
city
EmpName
Phone
11
john
smith
abc
Company 1
123
21
don
davis
def
Company 2
456
4
jim
jones
xyz
comapany4
001
How can i get desired output. With UNION i can't get "EmpName" and "Phone" column. Is there anyway to use JOIN to get desired output.
I think you still need UNION. Try this query:
SELECT ID, FirstName, LastName, city, EmpName, Phone
FROM table2
WHERE Status IN ('U', 'P')
UNION
SELECT t1.ID, t1.FirstName, t1.LastName, t2.city, t2.EmpName, t2.Phone
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t1.ID = t2.ID
WHERE t1.Group = 'E' AND t1.code = 'P'
;
First, your results are all coming from table2, so this returns the results specified in the question:
select t2.ID, t2.FirstName, t2.LastName, t2.city, t2.EmpName, t2.Phone
from table2 t2
where t2.status in ('U', 'P');
I think you also want a matching condition on table1 (which is not needed for your example). Based on your description:
select t2.ID, t2.FirstName, t2.LastName, t2.city, t2.EmpName, t2.Phone
from table2 t2
where t2.status in ('U', 'P') or
exists (select 1
from table1 t1
where t1.id = t2.id and
t1.Group = 'E' and t1.code = 'P'
);

join table for each variable

lets say i have 2 tables:
table 1:
PersonID PersonName
1 Micheal
2 Edward
3. Nord
4. Stephanie
Table 2
PurchaseID PurchaseItem. PersonID
1 Rack 1
2 Desk 1
3. Lamp 2
4. Table 3
with standard join, query result can return
1 Micheal Rack
2. Micheal Desk
3 Edward Lamp
4 Nord Table
but i need the result to be shown as:
1 Micheal Rack, Desk
2 Edward Lamp
3 Nord Table
You could group by the PersonName and use group_concat to concatenate the PurchaseItems:
SELECT t1.PersonId, PersonName, GROUP_CONCAT(PurchaseItem, ', ')
FROM t1
JOIN t2 ON t1.PersonId = t2.PersonId
GROUP BY t1.PersonId, PersonName
select t1.personid, t1.personname,
group_concat(t2.PurchaseItem)
from t1
join t2 on t1.personid = t2.personid
group by t1.personid, t1.personname
You can try something like this:
Select t1.personid, t1.personname, group_concat(t2.purchase_item)
from table1 t1 join table2 t2
on t1.personId = t2.personId
group by personID;

Appending row values of a table with columns of another table

I have a table T1 with standard columns like name, email, phone_number. The custom fields are store in custom_fields table as key, value.
I need to have key name of custom_fields as column name on actual table. How can I achieve this
Table T1
id name email
1 John john#example.com
2 Sam same#mydomain.com
custom_fields
id T1_id key value
1 1 Age 32
2 1 Job Title CEO
3 2 Age 40
4 2 Car Owned Ford EcoSport
Required
T1_id name email age job_title car_owned
1 John john#example.com 32 CEO Not specified
2 Sam same#mydomain.com 40 Not specified Ford EcoSport
You should use the table custom_fields two time one for job and one for car
select t1.id, t1.name, t1.email, t2.value, t3.value
from t1
inner join custom_fields t2 on t1.id = t2.t1_id and t2.key = 'Job Title'
inner join custom_fields t3 on t1.id = t3.t1_id and t3.key = 'Car Owned'

Counting unique numbers in a column MySQL

I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number

Need to concatenate results of a subquery in a select statement

I have three tables
Table1: Users
Columns: User_ID (int), FirstName, LastName....
Values
1 Jane Doe
2 John Doe
3 Mike Smith
Table2: User_Groups
Columns: User_ID (int), Group_ID(int)
Values:
Row1: 1 2
Row2: 1 3
Row3: 2 1
Row4: 2 3
Row5: 3 1
Table3: Groups
Columns: Group_ID (int), GroupName(varchar)
Values
Row1: 1 Admin
Row2: 2 Power User
Row3: 3 Developer
I would like to create a query that can return the results in the following way:
**RESULT
UserID GroupNames
Row1: 1 Power User, Developer
Row2: 2 Admin, Developer
Row3: 3 Admin
In SQL Server - I was able to achieve it using something like this:
SELECT User_ID,
SUBSTRING(
replace(
replace(
(SELECT Groups.GroupName
FROM User_Groups, Groups
where groups.Group_ID =
User_Groups.Group_ID AND
User_Groups.User_ID =Users.User_ID
FOR XML PATH('') )
,'<GROUPNAME>',', ')
,'</GROUPNAME>',''),3,2000) as UserGroups
FROM User_Groups LEFT JOIN Groups ON
User_Groups.Group_ID=Groups.Group_ID
ORDER BY User_ID ASC
I wanted to do get a similar final result in MySQL (tried GROUP_CONCAT etc) but unsuccessful.. how can I get similar **RESULT in MySQL. Please note the tables exist already and I cant change them.
Any help will be greatly appreciated
This works:
SELECT
t1.User_ID AS UserID,
(
SELECT
GROUP_CONCAT(t2.GroupName)
FROM
Groups t2
WHERE
t2.Group_ID IN(
SELECT
t3.Group_ID
FROM
User_Groups t3
WHERE
t3.iUser_ID = t1.User_ID
)
) AS GroupNames
FROM
Users t1
And this look like better idea, since you don't want to have user names, so that's no need to involve Users table:
SELECT
User_ID,
GROUP_CONCAT(GroupName) AS GroupNames
FROM
(
SELECT
t2.User_ID AS User_ID,
t3.GroupName AS GroupName
FROM
User_Groups t2
LEFT JOIN
Groups t3 ON (t3.Group_ID = t2.Group_ID)
) tmp
GROUP BY
User_ID