Is this MySQL multiple table join OK? - mysql

I have one table service_contacts which can contain listids from the lists table and contactids from the contacts table. contact_list_relationship has the relationships between contacts and lists tables.
I'm trying to pull all the contacts which could be in a contactid in service_contacts, or in a listid (which each list contains contactids).
SELECT d.* FROM service_contacts a
LEFT JOIN lists b
ON a.calllistid=b.listid
LEFT JOIN contact_list_relationship c
ON c.listid=b.listid
INNER JOIN contacts d
ON d.contactid=c.contactid OR d.contactid=a.contactid
WHERE a.memberid=12345
This runs, and pulls the expected results. So far ... I'm just wondering if there might be a better way.

Assuming that the fields you are joining on are indexed / or preferably defined with FOREIGN KEY constraints (which enforce indexing) you should be fine.
However MySQL does not always use indexes even if they are available. To check the indexes are being used you can run an explain on your statement i.e.
EXPLAIN
SELECT d.* FROM service_contacts a
LEFT JOIN lists b
ON a.calllistid=b.listid
LEFT JOIN contact_list_relationship c
ON c.listid=b.listid
INNER JOIN contacts d
ON d.contactid=c.contactid OR d.contactid=a.contactid
WHERE a.memberid=12345;
the results will inform you which indexes are being used in the statement. If the results are not as expected you can force Mysql to use the indexes withby stating 'force key for join indexname after each reference to a table.

Related

how can i make a view using the foreign key that is applies in mysql?

I am still a beginner and i searched in google ,read w3schools and couldn't get how to do it the following thing.
i want to create view from the info in cases field
AS you saw these are the info inside the tables (names,depart,idcards)
this the table i want to get the data from i made all the column start with id_* as foreign key for the PK in the previous tables
note:id_case is the PK of the table, id_dep is for department,id name is for name,id_complaint
This is just a bunch of joins:
CREATE VIEW viewname AS
SELECT *
FROM cases AS c
JOIN names AS n ON c.id_name = n.id
JOIN depart AS d ON c.id_dep = d.id
JOIN complaint AS cm ON c.id_complaint = cm.id
...
Note that if there are any column names that are the same among any of the tables, you'll need to list them explicitly in the SELECT list and assign distinct aliases to them. See MySQL JOIN tables with duplicate column names

Inner join of same table with conditions

So my problem is as follow, I have a table in MySQL with a UserId column and an ObjectId column (its a many to many relationship), and what I would like is to have is a query that gives me the list of objects that user X and Y share. Not sure how to make the joins to make this happen.
Use query something like below using self join
Select columns from table t1 join table t2 on t1.objectid=t2.objectid where t1.userid=X and t2.userid=Y

Suggestions to relate 3 SQL entities

I have 3 different entities in my relational database. The entity called 'Competencia' has many to many relation with two more entities 'Nivel', 'Funcion' or neither of them. 'CompetenciaTipo' is the field in 'Competencia' that tells me if 'Competencia' is related to 'Nivel, 'Funcion' or none of them. I have and intermediate table in both cases to work with those many-to-many-relations, you can look part of my ER diagram in the following image.
I will need to build a SQL query to bring all rows in 'Competencia' table and the relations with the other tables (if any) but it seems that it will be a complex query.
Do you think my model design is appropriate? Do you have any other suggestions to get the same result?
Thanks
Select Competencia Funcions:-
SELECT c.*, f.* --
FROM Competencia c
LEFT JOIN CompetenciaFuncion cf ON cf.CompetenciaId = c.CompetenciaId
LEFT JOIN Funcion f ON f.FuncionId = cf.FuncionId
WHERE {......insert additional conditions..... }
Select Competencia Nivels:-
SELECT c.*, n.* --
FROM Competencia c
LEFT JOIN CompetenciaNivel cn ON cn.CompetenciaId = c.CompetenciaId
LEFT JOIN Nivel n ON n.NivelId = cn.NivelId
WHERE {......insert additional conditions..... }
LEFT JOINs ensure that all of the details from the competencia table are returned and anything from the Funcion/Nivel table. NOTE: that most times you want to display the results in a grid (or similar), then the c.* part in the SELECT would be replaced by simply c.CompetenciaId, so that you have the key, but don't repeat the data from the competencia table unnecessarily.
These queries can also form the basis of VIEWs, if they are used over and over again.

SQL - Joining tables BUT not always

I need to perform a query SELECT that joins three tables (no problem with that). Nonetheless, the third table can, or NOT, have any element that match the joining KEY.
I want ALL data from the first two tables and if the ITEMS have ALSO information in the third table, fetch this data to.
For example, imagine that the first table have a person, the second table have his/her address (everyone lives anywhere), the third table stores the driving license (not everyone has this) - but I need to fetch all data whether or not people (all people) have driving license.
Thanks a lot for reading, if possible to give you suggestion / solution!
Use LEFT JOIN to join the third table. Using INNER JOIN a row has to exists. Using LEFT JOIN, the 'gaps' will be filled with NULLs.
SELECT
p.PersonID, -- NOT NULL
-- dl.PersonID, -- Can be null. Don't use this one.
p.FirstName,
p.LastName,
a.City,
a.Street,
dl.ValidUntilDate
FROM
Person p
INNER JOIN Addresse a ON a.AddressID = p.HomeAddressID
LEFT JOIN DrivingLicence dl ON dl.PersonId = p.PersonID

In what order are MySQL JOINs evaluated?

I have the following query:
SELECT c.*
FROM companies AS c
JOIN users AS u USING(companyid)
JOIN jobs AS j USING(userid)
JOIN useraccounts AS us USING(userid)
WHERE j.jobid = 123;
I have the following questions:
Is the USING syntax synonymous with ON syntax?
Are these joins evaluated left to right? In other words, does this query say: x = companies JOIN users; y = x JOIN jobs; z = y JOIN useraccounts;
If the answer to question 2 is yes, is it safe to assume that the companies table has companyid, userid and jobid columns?
I don't understand how the WHERE clause can be used to pick rows on the companies table when it is referring to the alias "j"
Any help would be appreciated!
USING (fieldname) is a shorthand way of saying ON table1.fieldname = table2.fieldname.
SQL doesn't define the 'order' in which JOINS are done because it is not the nature of the language. Obviously an order has to be specified in the statement, but an INNER JOIN can be considered commutative: you can list them in any order and you will get the same results.
That said, when constructing a SELECT ... JOIN, particularly one that includes LEFT JOINs, I've found it makes sense to regard the third JOIN as joining the new table to the results of the first JOIN, the fourth JOIN as joining the results of the second JOIN, and so on.
More rarely, the specified order can influence the behaviour of the query optimizer, due to the way it influences the heuristics.
No. The way the query is assembled, it requires that companies and users both have a companyid, jobs has a userid and a jobid and useraccounts has a userid. However, only one of companies or user needs a userid for the JOIN to work.
The WHERE clause is filtering the whole result -- i.e. all JOINed columns -- using a column provided by the jobs table.
I can't answer the bit about the USING syntax. That's weird. I've never seen it before, having always used an ON clause instead.
But what I can tell you is that the order of JOIN operations is determined dynamically by the query optimizer when it constructs its query plan, based on a system of optimization heuristics, some of which are:
Is the JOIN performed on a primary key field? If so, this gets high priority in the query plan.
Is the JOIN performed on a foreign key field? This also gets high priority.
Does an index exist on the joined field? If so, bump the priority.
Is a JOIN operation performed on a field in WHERE clause? Can the WHERE clause expression be evaluated by examining the index (rather than by performing a table scan)? This is a major optimization opportunity, so it gets a major priority bump.
What is the cardinality of the joined column? Columns with high cardinality give the optimizer more opportunities to discriminate against false matches (those that don't satisfy the WHERE clause or the ON clause), so high-cardinality joins are usually processed before low-cardinality joins.
How many actual rows are in the joined table? Joining against a table with only 100 values is going to create less of a data explosion than joining against a table with ten million rows.
Anyhow... the point is... there are a LOT of variables that go into the query execution plan. If you want to see how MySQL optimizes its queries, use the EXPLAIN syntax.
And here's a good article to read:
http://www.informit.com/articles/article.aspx?p=377652
ON EDIT:
To answer your 4th question: You aren't querying the "companies" table. You're querying the joined cross-product of ALL four tables in your FROM and USING clauses.
The "j.jobid" alias is just the fully-qualified name of one of the columns in that joined collection of tables.
In MySQL, it's often interesting to ask the query optimizer what it plans to do, with:
EXPLAIN SELECT [...]
See "7.2.1 Optimizing Queries with EXPLAIN"
Here is a more detailed answer on JOIN precedence. In your case, the JOINs are all commutative. Let's try one where they aren't.
Build schema:
CREATE TABLE users (
name text
);
CREATE TABLE orders (
order_id text,
user_name text
);
CREATE TABLE shipments (
order_id text,
fulfiller text
);
Add data:
INSERT INTO users VALUES ('Bob'), ('Mary');
INSERT INTO orders VALUES ('order1', 'Bob');
INSERT INTO shipments VALUES ('order1', 'Fulfilling Mary');
Run query:
SELECT *
FROM users
LEFT OUTER JOIN orders
ON orders.user_name = users.name
JOIN shipments
ON shipments.order_id = orders.order_id
Result:
Only the Bob row is returned
Analysis:
In this query the LEFT OUTER JOIN was evaluated first and the JOIN was evaluated on the composite result of the LEFT OUTER JOIN.
Second query:
SELECT *
FROM users
LEFT OUTER JOIN (
orders
JOIN shipments
ON shipments.order_id = orders.order_id)
ON orders.user_name = users.name
Result:
One row for Bob (with the fulfillment data) and one row for Mary with NULLs for fulfillment data.
Analysis:
The parenthesis changed the evaluation order.
Further MySQL documentation is at https://dev.mysql.com/doc/refman/5.5/en/nested-join-optimization.html
SEE http://dev.mysql.com/doc/refman/5.0/en/join.html
AND start reading here:
Join Processing Changes in MySQL 5.0.12
Beginning with MySQL 5.0.12, natural joins and joins with USING, including outer join variants, are processed according to the SQL:2003 standard. The goal was to align the syntax and semantics of MySQL with respect to NATURAL JOIN and JOIN ... USING according to SQL:2003. However, these changes in join processing can result in different output columns for some joins. Also, some queries that appeared to work correctly in older versions must be rewritten to comply with the standard.
These changes have five main aspects:
The way that MySQL determines the result columns of NATURAL or USING join operations (and thus the result of the entire FROM clause).
Expansion of SELECT * and SELECT tbl_name.* into a list of selected columns.
Resolution of column names in NATURAL or USING joins.
Transformation of NATURAL or USING joins into JOIN ... ON.
Resolution of column names in the ON condition of a JOIN ... ON.
Im not sure about the ON vs USING part (though this website says they are the same)
As for the ordering question, its entirely implementation (and probably query) specific. MYSQL most likely picks an order when compiling the request. If you do want to enforce a particular order you would have to 'nest' your queries:
SELECT c.*
FROM companies AS c
JOIN (SELECT * FROM users AS u
JOIN (SELECT * FROM jobs AS j USING(userid)
JOIN useraccounts AS us USING(userid)
WHERE j.jobid = 123)
)
as for part 4: the where clause limits what rows from the jobs table are eligible to be JOINed on. So if there are rows which would join due to the matching userids but don't have the correct jobid then they will be omitted.
1) Using is not exactly the same as on, but it is short hand where both tables have a column with the same name you are joining on... see: http://www.java2s.com/Tutorial/MySQL/0100__Table-Join/ThekeywordUSINGcanbeusedasareplacementfortheONkeywordduringthetableJoins.htm
It is more difficult to read in my opinion, so I'd go spelling out the joins.
3) It is not clear from this query, but I would guess it does not.
2) Assuming you are joining through the other tables (not all directly on companyies) the order in this query does matter... see comparisons below:
Origional:
SELECT c.*
FROM companies AS c
JOIN users AS u USING(companyid)
JOIN jobs AS j USING(userid)
JOIN useraccounts AS us USING(userid)
WHERE j.jobid = 123
What I think it is likely suggesting:
SELECT c.*
FROM companies AS c
JOIN users AS u on u.companyid = c.companyid
JOIN jobs AS j on j.userid = u.userid
JOIN useraccounts AS us on us.userid = u.userid
WHERE j.jobid = 123
You could switch you lines joining jobs & usersaccounts here.
What it would look like if everything joined on company:
SELECT c.*
FROM companies AS c
JOIN users AS u on u.companyid = c.companyid
JOIN jobs AS j on j.userid = c.userid
JOIN useraccounts AS us on us.userid = c.userid
WHERE j.jobid = 123
This doesn't really make logical sense... unless each user has their own company.
4.) The magic of sql is that you can only show certain columns but all of them are their for sorting and filtering...
if you returned
SELECT c.*, j.jobid....
you could clearly see what it was filtering on, but the database server doesn't care if you output a row or not for filtering.