join table for each variable - mysql

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;

Related

SQL - Count two columns together

I'm trying to count multiple columns as one column. For example:
Table 1 (Books):
ID
BookName
Genre
SubGenre
1
Name1
1
3
2
Name2
2
1
3
Name3
4
2
Table 2 (Genre):
ID
Genre
1
Horror
2
Drama
3
Romance
4
Sci-Fi
I want to be able to count the genre and subgenre as one to create a table of:
Result:
Genre
Count
Horror
2
Drama
2
Romance
1
Sci-Fi
1
Any help would be really appreciated. Thanks
Try below query-
SELECT t.Genre, Sum(t.cg) AS Count
FROM (
SELECT t2.Genre, Count(t1.Genre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.Genre
GROUP BY t2.Genre
UNION ALL
SELECT t2.Genre, Count(t1.SubGenre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.SubGenre
GROUP BY t2.Genre
) as t GROUP BY t.Genre;

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'
);

Select all IDs that have that value in MySQL

I have these two tables:
Table 1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
Table 2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
And I use the following query to get the Items that are like "A%" for each ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
I want to find a way to edit my query so I can get the above only for the IDs that happen to have item "F45" in them (but I don't want to use this item, just select the IDs that have it)..
Any help will be appreciated
I guess
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
Or adjust the subquery to select only needed rows (here I'm not sure what are you trying to achieve)

Finding all duplicate rows in a table

Let's say I have the following table
first last value
tony jones 1
james guy 5
sara miller 6
tony jones 2
sara miller 3
joe cool 4
david marting 7
Is there a command to quickly query all duplicate rows.
So I would end up with:
first last value
tony jones 1
tony jones 2
sara miller 6
sara miller 3
Help!
This is the query I need to add this to:
SELECT l.create_date, l.id, c.first_name, c.last_name, c.state, c.zipcode,
l.source AS 'affiliateId', ls.buyer, ls.amount FROM lead_status AS ls
INNER JOIN leads AS l ON l.id = ls.lead_id
INNER JOIN contacts AS c ON c.lead_id = l.id
WHERE ls.discriminator = 'AUTO_POST' AND l.affiliate_id=1003
AND ls.winner =1 AND l.test =0 AND l.create_date BETWEEN '2011-10-03' AND '2011-10-19';
SELECT t1.*
FROM <table> t1, <table> t2
WHERE t1.first=t2.first AND
t1.last=t2.last AND
t1.value<>t2.value
;
This should do it efficiently, assuming proper indexing and that the value is unique for each row:
SELECT first,
last,
value
FROM MyTable AS T1
WHERE EXISTS ( -- Is there another row with the same first/last name,
SELECT * -- but a different value
FROM MyTable AS T2
WHERE T2.first=T1.first
AND T2.last=T1.last
AND T2.value<>T1.value
)
If you only want repeated....
select t2.first, t2.last , t1.value from (
select first, last, count(*) from your_table
group by first, last
having count(*)>1
) t2 inner join your_table t1 on (t1.first=t2.first and t1.last=t2.last)
Additionally,
select first, last , count(*) from your_table
group by first, last
Will return first, last and an extra column with the number of times the record is repeated
How about just using "SELECT DISTINCT ..." ?
Doesn't it do the job?

MySQL statement: spliting rows values into new columns

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