table user
+-----+-------+----------+------------+
| id | name | gender | computer |
+-----+-------+----------+------------+
| 1 | ben | 1 | 3 |
+-----+-------+----------+------------+
table translate
+-----+--------+
| id | trans |
+-----+--------+
| 1 | boy |
| 2 | girl |
| 3 | pc |
| 4 | mac |
+-----+--------+
final result
+-----+-------+----------+----------+
| id | name | gender | computer |
+-----+-------+----------+----------+
| 1 | ben | boy | pc |
+-----+-------+----------+----------+
I have 2 tables, one is user's data, the other is translate.
I want to use translate table to translate user's gender & computer's values
anyone know how to achieve this?
Use JOIN.
Query
select
t1.`id`,
t1.`name`,
t2.`trans` as `gender`,
t3.`trans` as `computer`
from `user` t1
join `translate` t2
on t1.`gender` = t2.`id`
join `translate` t3
on t1.`computer` = t3.`id`;
The way you've presented this, it doesn't appear to be a translation problem (in the sense of language), but rather a standard database partition problem. A more standard solution would be to define your entities each in their own tables:
user
+-----+--------+------------+--------------+
| id | name | genderId | computerId |
+-----+--------+------------+--------------+
| 1 | ben | 1 | 1 |
+-----+--------+------------+--------------+
gender
+-----+--------+
| id | name |
+-----+--------+
| 1 | boy |
+-----+--------+
| 2 | girl |
+-----+--------+
computer
+-----+--------+
| id | name |
+-----+--------+
| 1 | pc |
+-----+--------+
| 2 | mac |
+-----+--------+
...and then your query reads:
SELECT *
FROM user
INNER JOIN gender ON user.genderId = gender.id
INNER JOIN computer ON user.computerId = computer.id
Related
I am trying to select data from multiple tables. The main table is Notices.
Table: Notices
+-------+-------------+----------------+
| id | notice_code | company_number |
+-------+-------------+----------------+
| 96008 | 2410 | 09844265 |
| 96014 | 2450 | 02640968 |
| 96032 | 2443 | 03666759 |
+-------+-------------+----------------+
I have to select related information for the rows for Table Notice from different tables. Below are the other 4 tables and their relation with table Notices
Table Companies has a direct relation with Table Notices
Table: Companies
Companies.Company_number = Notices. Company_ Number
+----------------+--------------+-------+
| company_number | company_name | sic1 |
+----------------+--------------+-------+
| 02640968 | XYZ Logistic | 28220 |
| 03666759 | OPQ Logistic | 41100 |
| 09844265 | ABC Logistic | 49410 |
+----------------+--------------+-------+
Table Sic_codes doesn’t have a direct relation with Table Notices. But it has with Table Companies.
Table: Sic_Codes.
Companies.Sic1 = Sic_code.Code
+-------+----------------+
| code | sector |
+-------+----------------+
| 28220 | Manufacture |
| 41100 | Construction |
| 49410 | Transportation |
+-------+----------------+
Table Insovency_Practionar does not have a direct relation with Table Notices. There is another Table Notice_insolvency_practitionar_ID to create a relation between these two tables Table Insovency_Practionar and Table Notices
Table: Notice_insolvency_practitionar_ID .
Notice_insolvency_practitionar_ID. Notice_ID = Notices. ID
+-----------+----------------------------+
| notice_id | insolvency_practitioner_id |
+-----------+----------------------------+
| 96008 | 1048 |
| 96008 | 725 |
| 96032 | 548 |
+-----------+----------------------------+
Table: Insovency_Practionar .
Insovency_Practionar.ID = Notice_insolvency_practitionar_ID. Insolvency_Practiotionar_ID
+------+---------+
| id | name |
+------+---------+
| 548 | Charlie |
| 725 | Bill |
| 1048 | Andrew |
+------+---------+
My expected output is the following: where company name will be coming from table company; sic1 and sector will come from table sic_code and practitioner will come from table Insovency_Practionar
+----------------+--------------+-------+----------------+--------------+
| company_number | company_name | sic1 | sector | practitioner |
+----------------+--------------+-------+----------------+--------------+
| 9844265 | ABC Logistic | 49410 | Transportation | Andrew |
| 2640968 | XYZ Logistic | 28220 | Manufacture | Bill |
| 3666759 | OPQ Logistic | 41100 | Construction | Charlie |
+----------------+--------------+-------+----------------+--------------+
I have used LEFT Join in my QUERY.
Here is my query
SELECT n.company_number
, c.company_name
, c.sic1
, s.sector
,i.name practitioner
FROM notices n
LEFT JOIN companies c
ON c.company_number = n.company_number
LEFT JOIN sic_codes s
ON s.code = c.sic1
LEFT JOIN notice_insolvency_practitioners ni
ON ni.notice_id = n.id
LEFT JOIN insolvency_practitioners i
ON i.id = ni.insolvency_practitioner_id
where n.notice_code =2410
Here is my SQL Fiddle. http://sqlfiddle.com/#!9/4887e2/2
I wanted to know if my query was right. Or is there any other better way to write the query. As initially, the query gave me wrong result when I was testing.
Update: I have figured out there was a duplicate entry. And that was the reason for not getting the expected output. I have now corrected that. But still want to know if my query is right or is there a better way to write the query
I have figured out what the problem was. Thanks to #Viney
<pre>
+-----------+----------------------------+
| notice_id | insolvency_practitioner_id |
+-----------+----------------------------+
| 96008 | 1048 |
| 96008 | 725 |
| 96032 | 548 |
+-----------+----------------------------+
</pre>
duplicate entry for notice id 96008. It should be like
<pre>
+-----------+----------------------------+
| notice_id | insolvency_practitioner_id |
+-----------+----------------------------+
| 96008 | 1048 |
| 96014 | 725 |
| 96032 | 548 |
+-----------+----------------------------+
</pre>
I have also edited My question and have reduced the extra columns to make it readable for users
I have a table like this.
+------------+-------------+--------------+
| name | hobby | hobby_number |
+------------+-------------+--------------+
| jack | sport | 1 |
| marco | skydiving | 3 |
| alfonso | driving | 1 |
| marco | learning | 2 |
| jack | dancing | 2 |
+------------+-------------+--------------+
I want to use sql select statement to select only one unique name.
The table I want may look like this:
+------------+-------------+--------------+
| name | hobby | hobby_number |
+------------+-------------+--------------+
| jack | sport | 1 |
| marco | learning | 2 |
| alfonso | driving | 1 |
+------------+-------------+--------------+
What should sql query be?
Thank you in advance.
select t.* from your_table t
inner join
(
select name, min(hobby_number) as minh
from your_table
group by name
) x on x.name = t.name and x.minh = t.hobby_number
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
I'm new to relational sql. I'm trying to figure out a query to return the names of customers who have more than one type of account.
customers:
+------------+--------------+
| cid | Name |
+------------+--------------+
| 1 | Bob |
| 2 | John |
| 3 | Jane |
+------------+--------------+
accounts:
+------------+--------------+
| aid | type |
+------------+--------------+
| 1 | Checking |
| 2 | Saving |
| 3 | CD |
+------------+--------------+
transactions:
+------------+--------------+--------------+
| tid | cid | aid |
+------------+--------------+--------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 3 |
| 5 | 3 | 1 |
+------------+--------------+--------------+
With these tables, the query should return Bob and John. I'm having some trouble with how to write such a query. More specifically, how do I keep count of how many accounts a customer has and how do I compare if the accounts are different without adding a new column to the table?
Okay, this seems to work in SQL Fiddle with my test data structure. Try it out with your real data structure and see if it gives you what you're looking for.
SELECT name FROM customers c WHERE EXISTS(
SELECT DISTINCT aid FROM transactions
WHERE cid = c.cid
HAVING COUNT(DISTINCT aid)>1
)
I have two tables where I'm trying to select rows where a common field between those tables matches exactly, however it's proving difficult to write the query. Here is a simplified version:
The tables look like this (simplified):
T1:
id, name, sn
T2:
id, location, sn
I'm trying to get t1.name and t2.loc together only where t1.sn=t2.sn. The sn field is unique in both, and so at the most, only 1 record will match between tables. In t1, all records have a sn field value, however in t2 about 30% of them have NULL for sn. So, I an expecting the join to produce somewhat fewer rows than t1 has.
How would I do the join?
Thanks.
Sample data:
t1:
+---+--------+-------+-----+
| id| name | sn | ... |
+---+--------+-------+-----+
| 1 | thing1 | 12345 | |
| 2 | thing2 | 10000 | |
| 3 | thing3 | 33445 | |
| 4 | thing4 | 99223 | |
+---+--------+-------+-----+
T2:
+----+--------+-------+-----+
| id | loc | sn | ... |
+----+--------+-------+-----+
| 90 | here | 12345 | |
| 92 | there | NULL | |
| 96 | near | 33445 | |
| 99 | far | 99223 | |
+----+--------+-------+-----+
Result:
+--------+-------+-------+
| name | loc | sn |
+--------+-------+-------+
| thing1 | here | 12345 |
| thing3 | near | 33445 |
| thing4 | far | 99223 |
+--------+-------+-------+
SELECT
t1.name AS name,
t2.loc AS loc,
t1.sn AS sn
FROM t1
INNER JOIN t2 ON t1.sn=t2.sn