List of persons with fields from different tables - mysql

I'm a newbie with relational databases. My client asked for a list of clients with different fields by default. But in the schema are like 8 different tables and I need to make the list with fields for each one.
I was thinking of implementing a query like this one.
SELECT name, surname, country_of_birth, nationality, email
from clients,
client_documents as documents,
client_addresses as address
There are duplicate fields, but I don't know how to use the same uuid_client with it's the foreign key in each table.

you can use join and shorter table aliases so it will be easier for you to include its columns.
select t1.name
, t1.surname
, t1.country_of_birth
, t1.nationality
, t1.email
, t2.documents
, t3.city
from clients t1
inner join client_documents t2 on t1.uuid_client = t2.uuid_client
inner join client_addresses t3 on t1.uuid_client = t3.uuid_client

SELECT name, surname, country_of_birth, nationality, email
from clients, client_documents as documents, client_addresses as address
From the query above it seems you need to join the tables. But you didn't mention any of the fields from the remaining tables. So, if you have any common fields in these tables it is possible to use joins
Or Else you can try using UNION too.
Please Elaborate your question. So that I can answer it properly

One way to solve it is using JOIN.
For example :
SELECT clients.name, clients.surname, address.country_of_birth, clients.nationality, clients.email
FROM clients clients
LEFT JOIN client_documents AS documents on clients.id = documents.client_id
LEFT JOIN client_addresses AS address on clients.id = address.client_id
I suggest LEFT JOIN for this case, because you cannot see clients do not have address or documentation info if we use (INNER) JOIN. So we used LEFT join, but as I said the choice depends on your scenario.

Related

How to use join to link values from one table to another

I have two tables.
drivers
name number email
requests
id driverassigned ....
I want to get everything from drivers table that may or may not be mentioned in requests.driverassigned.
I have tried using join but it returns rows that have a match. Here is what I have so far.
select drivers.email
, drivers.`number`
, drivers.name
, requests.id
from drivers join requests on drivers.`number` like requests.driverassigned
I am sure there is a common solution but I don't have enough sql knowledge to fish it out.
Any Suggestions?
use left join
select drivers.email, drivers.`number`, drivers.name,
coalesce(requests.id,'none') as request_id
from drivers left join requests
on drivers.`number` like '%'+requests.driverassigned+'%'
select drivers.email, drivers.`number`, drivers.name, requests.id
from drivers left join requests on drivers.`number` = requests.driverassigned

Mysql JOIN syntax error getting specified columns data

I have 2 tables, one is setting and one is accounts
setting has columns of: isVerified, customMessage, user
accounts has columns of: id, fullName, password, address, phone
I know I have to do join but how do I get only fullName from accounts table?
I did this
SELECT isVerified, customMessage, fullName
FROM setting FULL OUTER JOIN
accounts
ON setting.user = accounts.id;
but got error near the JOIN. What's wrong?
An inner join should suffice:
SELECT s.isVerified, s.customMessage, a.fullName
FROM setting s INNER JOIN
accounts a
ON s.user = a.id;
MySQL does not support FULL OUTER JOIN. Presumably, all accounts have settings and vice versa.
Note that I introduced table aliases so the query is easier to write and to read. And, in this query, all column names specify the table they come from.
I know I have to do join but how do I get only fullName from accounts
table?
If you only want fullName only specify fullName column in your select statement.
Select fullname FROM ....
As others have pointed out MySQL doesn't support FULL OUTER JOIN so change that to simply JOIN as Gordon Linoff has mentioned above.
Normally when you do a join you either want rows that match both the tables (setting and accounts in your case). Based on the columns you've described and depending on how you've designed your schema it's either a One to One relationship between two tables or One to Many. Your case sounds like one to one as each users account will have a setting.
You're joining on s.user = a.id but I don't see you mentioned s.user is actually same as a.id? What is the user field? Perhaps you need to name this better as s.id if it's actually an id. As others have pointed out please include your actual table definition so it's easier to figure out why you get the SQL error while running your query.
Good luck.

mysql request retrieve data combined with 2 tables

I have three tables
I would like to request all persons, where the companyTypeID is for example '2'. How can I query that?
You could use the in operator:
SELECT *
FROM persons
WHERE CompanyId IN (SELECT CompanyId
FROM company
WHERE CompanyTypeId = 2)
Do an INNER JOIN (left or right joins are functionally similar, the only difference is which side of the equation is honoured). Nested queries / subqueries are extremely expensive if they become dependent in nature—even though that's not the scenario in your case—and I do not recommend using them for large tables.
SELECT t1.*
FROM Persons AS t1
LEFT JOIN Company AS t2 ON
t2.companyTypeID = t1.CompanyID
To ensure that you are using an index for joining, you should create indexes on the companyTypeID and CompanyID columns of each table. Prepend EXPLAIN EXTENDED to the query above to verify that the indexes are indeed being used.
You need to use SQL statement JOIN. It's all about mathematical sets!
A Graphic (and superficial) explanation about JOINs statement under mathematical sets approach:
https://www.google.com.br/imgres?imgurl=http%3A%2F%2Fi.imgur.com%2FhhRDO4d.png&imgrefurl=https%3A%2F%2Fwww.reddit.com%2Fr%2Fprogramming%2Fcomments%2F1xlqeu%2Fsql_joins_explained_xpost_rsql%2F&docid=q4Ank7XVw8j7DM&tbnid=f-L_7a3HkxW_3M%3A&w=1000&h=740&client=ubuntu&bih=878&biw=1745&ved=0ahUKEwj2oaer5evPAhUBFZAKHe4nAdAQMwgdKAEwAQ&iact=mrc&uact=8#h=740&w=1000
SQL for your problem(If I got this):
SELECT * FROM Persons p LEFT JOIN Company c ON c.ID = p.companyID
LEFT JOIN CompanyType ct ON ct.ID = c.companyTypeID
WHERE c.id = 2;
Why 'LEFT JOIN'? Checkout the the link about set approach explanation above!
The second 'LEFT JOIN' is just to bring the description of companyType table.
The "*" in statement is didatic! You must not use this in production for the good of performance. Therefore, you must to replace the '*' with all the fields you need. For example, "CONCAT(p.firstname,CONCAT(" ",p.lastname)) as PersonName, c.name CompanyName,..." or something like that. I suppose you're using MySQL
Hope I've helped!
Perform an SQL join:
Joins are quicker than the subquery, in the other post:
SELECT Persons.firstname AS first name
FROM Persons
JOIN Company ON company.ID == Persons.CompanyID
WHERE Company.companyTypeID == 2
Although you will need to select all he fields you want, using alias to simplify the names

Select specific fields from two inner joins

Given tables Contracts, Contract_Plans and Contract_Plan_Tags, can I select specific fields from all three within an inner join query?
Currently, I have
SELECT * FROM Contracts
INNER JOIN Contract_Plans
ON Contracts.ContractNum = Contract_Plans.ContractNum
INNER JOIN Contract_Plan_Tags
ON Contracts.ContractNum = Contract_Plan_Tags.ContractNum
WHERE Contract_Plan_Tags.tag_id = 44 OR Contract_Plan_Tags.tag_id = 45
This query returns all the fields, but is there any way to select the specific fields from the join tables?
I know I can do
SELECT ContractNum, ContractName FROM Contracts
...
...
but that only selects fields from Contracts, not the other tables.
You should learn about table aliases. The best table aliases are short abbreviations of the tables. You can then list the fields with the aliases:
SELECT c.ContractNum, c.ContractName, cpt.tag_id, . . .
FROM Contracts c
INNER JOIN Contract_Plans cp
ON c.ContractNum = cp.ContractNum
INNER JOIN Contract_Plan_Tags cpt
ON c.ContractNum = cpt.ContractNum
WHERE cpt.tag_id in (44, 45)
I notice that you use table names in the where clause, so I hope I understand the question correctly. You can use aliases (or table names) in the select and where (and group by and having) clauses.
You must prefix the fields by their table name if order for MySQL to distinguish them:
SELECT Contracts.This, Contract_Plan_Tags.That, Contract_Plans.There FROM Contracts
INNER JOIN Contract_Plans
ON Contracts.ContractNum = Contract_Plans.ContractNum
INNER JOIN Contract_Plan_Tags
ON Contracts.ContractNum = Contract_Plan_Tags.ContractNum
WHERE Contract_Plan_Tags.tag_id = 44 OR Contract_Plan_Tags.tag_id = 45
Yes, you can select any field from any table from the FROM clause. If two fields of two tables have the same name, then you must prefix the field with the name of the table (or else you get an error from the parser: "field name is ambiguous").
You may prefix the unambiguous field names too if you find it more readable.
In fact it works exactly the same way as in the WHERE clause.
Incidentally, the same requirement exists if you join tables from several databases. If two tables have the same name, you must prefix their name with the database name, but do not need to if the name is unambiguous.
Oh and you can also do this: SELECT table1.*, table2.some_field, table3.* FROM...

Database design question - Performance needed

I created an inventory system which mostly is using a server-sided scripting language to do all the work. To try and get some performance gains I am looking to better design my database to try and minimizing the scripts.
I have a table named metal_part which has a one to one relationship with five other tables, basically the other tables are other parts, which those parts then have a one to one relationship with a few other tables.
When I query metal_part I need all the UPC numbers from each table, so its direct one to one relationships need to get their own information from their direct one to one relationship tables ect... Is it possible to make a huge query to build it all and put it in a form at like:
(###) - ####/##/##/## [a-z]
Using a query? or do I have to get all the information and concat it using a scripting language?
Thanks
You should be able to get all of the information you need using a standard join, and then, with the concat function appropriate to your database (see here http://www.1keydata.com/sql/sql-concatenate.html) you can form the string you want.
Your question is very vague.
I'm guessing you are talking about matching on a primary key called partnumber or something like that.
You can do this using a query like
SELECT mp.partnumber
, mp.UPC_number
, wp.UPC_number
, pp.UPC_number
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127';
You can also do
SELECT mp.partnumber
, group_concat(mp.UPC_number) as metal_UPCs
, group_concat(wp.UPC_number) as wood_UPCs
, group(concat(pp.UPC_number) as plastic_UPCs
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;
or
SELECT mp.partnumber
, concat_ws(','
, group_concat(mp.UPC_number)
, group_concat(wp.UPC_number)
, group(concat(pp.UPC_number)
) as UPCs_of_parts
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;