I'm stuck in joining two tables. My case is very unique. I have two tables as below:
Table user
+---------+----------+--------------+
|user_id | fname | factory_ids |
+---------+----------+--------------+
| 1 | Andrew| 1,2,3 |
| 2 | Roberts | 2,2 |
+---------+----------+--------------+
Table factory
+------------+----------+
|factory_id | fname |
+------------+----------+
| 1 | F1 |
| 2 | F2 |
| 3 | F3 |
| 4 | F4 |
+------------+----------+
Now, if I want to select all user data and display their factory id and factory name, how to display the factory_name, if the factory_ids are 1,2,3? I want it to display factory_name, F1,F2,F3?
Anyone can help?
Related
Let's say I have two tables, one with firstnames, and another with lastnames:
firstnametable
---------------------------------------------
| id | firstname | |
---------------------------------------------
| 1 | John | |
| 2 | Pete | |
---------------------------------------------
lastnametable
---------------------------------------------
| id | lastname | firstname_id | active |
---------------------------------------------
| 1 | Dough | 1 | 0 |
| 2 | Do | 1 | 1 |
| 2 | Sampres | 2 | 1 |
---------------------------------------------
I am looking for Query such that it would appear as if it came from a single table:
firstnametable
---------------------------------------------
| id | firstname | lastname | |
---------------------------------------------
| 1 | John | Do | |
| 2 | Pete | Sampres | |
---------------------------------------------
But it is important for me that the results are delivered in such a fashion that the prefix is also as if the results are all from the original table, ie:firstnametable.firstname and firstnametable.lastname
Is this at all possible?
My current solution is:
SELECT firstnametable.id,
firstnametable.firstname,
CONCAT(jointable.name) as lastname
FROM firstnametable
LEFT JOIN (
SELECT lastname
FROM lastnametable
WHERE active = 1
) as jointable ON (firstnametable.id = jointable.firstname_id)
But I am still missing my desired firstnametable prefix for the lastname column.
You need to include the firstname id in the jointtable select and I don't know what the purpose of the concat is since you aren't concating anything but you haven't selected name anywhere perhaps you meant lastname. If you want to include the table name in the headers use an alias.
SELECT firstnametable.id as 'firstnametable.id',
firstnametable.firstname as 'firstnametable.firstname',
jointable.lastname as 'firstnametable.lastname'
FROM firstnametable
LEFT JOIN (
SELECT firstname_id,
lastname
FROM lastnametable
WHERE active = 1
) as jointable ON firstnametable.id = jointable.firstname_id;
+-------------------+--------------------------+-------------------------+
| firstnametable.id | firstnametable.firstname | firstnametable.lastname |
+-------------------+--------------------------+-------------------------+
| 1 | John | Do |
| 2 | Pete | Sampres |
+-------------------+--------------------------+-------------------------+
2 rows in set (0.001 sec)
I have this tables People, Ticket, and Report.
+----------+-------+-----+
| idPeople | Name | Age |
+----------+-------+-----+
| 1 | Name1 | 21 |
| 2 | Name2 | 37 |
| 3 | Name3 | 28 |
+----------+-------+-----+
I would like to replace the ForeingKey idPeople with columns Name and Age from People table.
+----------+------------+------------+----------+
| idTicket | ticketCol2 | ticketCol3 | idPeople |
+----------+------------+------------+----------+
| 5 | True | 01/06/99 | 1 |
| 6 | False | 01/06/99 | 2 |
| 7 | True | 01/06/99 | 4 |
+----------+------------+------------+----------+
In the Report table replace the Foreing Key idTicket with ticketCol2, Name, Age from the previous table Ticket with replaced columns (idPeople by Name, Age).
+----------+----------+------------+------------+
| idReport | idTicket | ReportCol3 | ReportCol4 |
+----------+----------+------------+------------+
| 1 | 5 | 01/06/99 | blabla |
| 2 | 7 | 01/06/99 | asdfdd |
| 2 | 6 | 01/06/99 | fooboo |
+----------+----------+------------+------------+
And I the result should be like this table and must be done in one query.
+----------+------------+------------+------------+------------+------+-----+
| idReport | ticketCol2 | ticketCol3 | ReportCol3 | ReportCol4 | Name | Age |
+----------+------------+------------+------------+------------+------+-----+
| 1 | 01/06/99 | abcd | blabla | 123456 | Name | 20 |
| 2 | 01/06/99 | bcda | asdfdd | 321456 | Name | 23 |
| 3 | 01/06/99 | asdf | fooboo | 123456 | Name | 28 |
+----------+------------+------------+------------+------------+------+-----+
I Have tried replacing the foreingkeys with LEFT JOIN and bringing some columns Name and Age to the Ticket table but now the last part where I should replace idTicket with Columns from Ticket is not working.
I have read about the nested JOINs but I cannot understand it very well, I would really appreciate some idea of how I can do it or what should I investigate. Are nested Joins the right way?
The query that I've tried to accomplish the Table Ticket.
SELECT Ticket.ticketCol2, Ticket.ticketCol3, p.Name 'Name', p.Age 'Age'
from Ticket
left join people p on (Ticket.idPeople=p.idPeople);
Try something like this:
SELECT Report.idReport,
Ticket.ticketCol2,
Ticket.ticketCol3,
Report.ReportCol3,
Report.ReportCol4,
People.Name,
People.Age
FROM People
LEFT JOIN Ticket ON Ticket.idPeople = People.idPeople
LEFT JOIN Report ON Report.idTicket = Ticket.idTicket
Like #RiggsFolly said, the Ticket.idPeople won´t match to the People.idPeople, so this will not match any rows.
i have 2 tables.
1.home_shop
+---+------------+
|id | product |
+---+------------+
| 1 | soap |
| 2 | cake |
| 3 | biscuit |
+---+------------+
2.office_shop
+---+------------+
|id | product |
+---+------------+
| 1 | key |
| 2 | lock |
| 3 | pen |
+---+------------+
what i want is union this two tables into a new table "complete_shop" with a flag indicating "home" and "office"
for example:
+---+------------+-------------+
|id | product | flag |
+---+------------+-------------+
| 1 | soap | home |
| 1 | key | office |
| 2 | cake | home |
| 2 | lock | office |
| 3 | biscuit | home |
| 3 | pen | office |
+---+------------+-------------+
how do i do this union in mysql please help me. i am a beginner
Do a UNION query and introduce the flag column using the appropriate values.
SELECT id, product, 'home' AS flag
FROM home_shop
UNION ALL
SELECT id, product, 'office' AS flag
FROM office_shop
ORDER BY id, flag
Note that you don't need to use a subquery to order here, you can just specify the columns you want to use.
I have the following sample data:
| key_id | name | name_id | data_id |
+--------+-------+---------+---------+
| 1 | jim | 23 | 098 |
| 2 | joe | 24 | 098 |
| 3 | john | 25 | 098 |
| 4 | jack | 26 | 098 |
| 5 | jim | 23 | 091 |
| 6 | jim | 23 | 090 |
I have tried this query:
INSERT INTO temp_table
SELECT
DISTINCT #key_id,
name,
name_id,
#data_id FROM table1,
I am trying to dedupe a table by all fields in a row.
My desired output:
| key_id | name | name_id | data_id |
+--------+-------+---------+---------+
| 1 | jim | 23 | 098 |
| 2 | joe | 24 | 098 |
| 3 | john | 25 | 098 |
| 4 | jack | 26 | 098 |
What I'm actually getting:
| key_id | name | name_id | data_id |
+--------+-------+---------+----------+
| 1 | jim | 23 | NULL |
| 2 | joe | 24 | NULL |
| 3 | john | 25 | NULL |
| 4 | jack | 26 | NULL |
I am able to dedupe the table, but I am setting the 'data_Id' value to NULL by attempting to override the field with '#'
Is there anyway to select distinct on all fields and while keeping the value for 'data_id'? I will take the highest or MAX data_id # if possible.
If you only want one row returned for a specific value (in this case, name), one option you have is to group by that value. This seems like a good approach because you also said you wanted the largest data_id for each name, so I would suggest grouping and using the MAX() aggregate function like this:
SELECT name, name_id, MAX(data_id) AS data_id
FROM myTable
GROUP BY name, name_id;
The only thing you should be aware of is the possibility that a name occurs multiple times under different name_ids. If that is possible in your table, you could group by the name_id too, which is what I did.
Since you stated you're not interested in the key_id but only the name, I just excluded it from the query altogether to get this:
| name | name_id | data_id |
+-------+---------+---------+
| jim | 23 | 098 |
| joe | 24 | 098 |
| john | 25 | 098 |
| jack | 26 | 098 |
Here is the SQL Fiddle example.
RENAME TABLE myTable to Old_mytable,
myTable2 to myTable
INSERT INTO myTable
SELECT *
FROM Old_myTable
GROUP BY name, name_id;
This groups my tables by the values I want to dedupe while still keeping structure and ignoring the 'Data_id' column
I know there are similar questions out there but I can't find this particular case among them. If someone knows where this is answered please hook me up with a link. Otherwise here goes.
I have a table which has two fields I'm interested in - code, and id. None of these are unique though there is a company field which combines with the id field to make the primary key.
I need a list of all codes and names which have more than one name associated with the same code. so if my date looks like this:
| code | name | company |
+---------------+---------------+----------------------------------------------------+
| 00009 | name | 1 |
| 00009 | name | 2 |
| 00009 | name | 3 |
| 00009 | name | 4 |
| 00009 | diff name | 1 |
| 00014 | Foo | 2 |
| 00014 | foo | 3 |
| 00014 | foo | 4 |
| 00014 | foo | 5 |
| 00014 | foo | 6 |
| 00015 | barbaz | 1 |
| 00015 | barbaz | 2 |
| 00015 | barbaz | 3 |
| 00015 | barbaz | 4 |
| 00015 | bar baz | 5 |
| 00017 | foo | 1 |
| 00018 | bar | 1 |
I need my results too look like this:
| code | name |
+---------------+-------------------------------------------------------------------+
| 00009 | name |
| 00009 | diff name |
| 00014 | Foo |
| 00014 | foo |
| 00015 | barbaz |
| 00015 | bar baz |
I have tried several things including
SELECT DISTINCT t1.code, t1.name from items t1
inner join (select t2.code from items t2 group by t2.name ) t2
on (t1.code = t2.code)
group by t1.code order by t1.code;
Which of course is wrong. Thanks for any insight!
[edit] I forgot to mention one detail. I only want to list results that have more than one unique name entry for a give code. I've updated the initial data (which does not change my desired results).
[edited for typos]
Use HAVING with COUNT(DISTINCT):
SELECT code, name
FROM items
WHERE code IN (
SELECT code
FROM items
GROUP BY code
HAVING count(DISTINCT name) > 1
) t1
ORDER BY code, name