I am not sure why but this query (even when there are two results) is taking too long to execute.
What I am trying to do is that users c table's UID (primary key) matches with entity_id of all the other tables (foreign key). That's all I have to do.
I guess the problem is that there are too many foreign keys I have to match my primary key.
SELECT
f.field_fmno_value as fo,
fn.field_full_name_value as fullname,
c.mail as email,
t.field_type_value as persontype,
ti.field_title_value as persontitle,
sc.field_skill_code_value as skillcode,
sg.field_skill_group_value as skillgroup,
dn.field_department_value as deptt,
r.field_region_for_real_value as region,
cl.field_region_value as citylocation,
goc.field_goc_office_value as goc,
hu.field_hub_value as hub,
ge.field_gender_value as gender,
ft.field_full_tenure_value as tenure,
tsa.field_tenure_since_associate_value as tass,
b.category as category,
b.subcategory as subcategory,
b.section as industry,
b.title as title,
a.browser_platform, b.path, b.title, a.referer, a.time, a.id
FROM
track_da_files a, track_da_files_paths b, users c, field_data_field_region_for_real r,
field_data_field_fo f,
field_data_field_full_name fn,
field_data_field_type t,
field_data_field_title ti,
field_data_field_s_code sc,
field_data_field_s_group sg,
field_data_field_department dn,
field_data_field_region cl,
field_data_field_g_office goc,
field_data_field_hub hu,
field_data_field_gender ge,
field_data_field_full_ten ft,
field_data_field_ten_since_associate tsa
where a.pid = b.pid AND a.uid = c.uid AND a.uid <> 0
AND c.uid = r.entity_id
AND c.uid = fn.entity_id
AND c.uid = f.entity_id
AND c.uid = t.entity_id
AND c.uid = ti.entity_id
AND c.uid = sc.entity_id
AND c.uid = sg.entity_id
AND c.uid = dn.entity_id
AND c.uid = cl.entity_id
AND c.uid = goc.entity_id
AND c.uid = hu.entity_id
AND c.uid = ge.entity_id
AND c.uid = ft.entity_id
AND c.uid = tsa.entity_id
GROUP a.pid
ORDER BY 1 DESC
Revised query to JOIN format... Also, GROUP BY appears irrelevant as you are not doing any aggregations. If you have multiple records possible at any of the child-level tables, that could force multiple records, but only for those instances. If everything is a 1:1 ratio of records, you should be fine. If 1:n ration in more than 1 child table you will get a Cartesian result and could bloat (such as multiple skill groups).
Also, since a.uid = c.uid, you could also use the a.uid to all the underlying tables too so they all appear as child-tables. I would assume all these child tables are lookup tables and ALL of them have an index on the respective field representing the "uid" it is joined based on.
SELECT
f.field_fmno_value as fo,
fn.field_full_name_value as fullname,
c.mail as email,
t.field_type_value as persontype,
ti.field_title_value as persontitle,
sc.field_skill_code_value as skillcode,
sg.field_skill_group_value as skillgroup,
dn.field_department_value as deptt,
r.field_region_for_real_value as region,
cl.field_region_value as citylocation,
goc.field_goc_office_value as goc,
hu.field_hub_value as hub,
ge.field_gender_value as gender,
ft.field_full_tenure_value as tenure,
tsa.field_tenure_since_associate_value as tass,
b.category as category,
b.subcategory as subcategory,
b.section as industry,
b.title as title,
a.browser_platform,
b.path,
b.title,
a.referer,
a.time,
a.id
FROM
track_da_files a
JOIN track_da_files_paths b
ON a.pid = b.pid
JOIN users c
ON a.uid = c.uid
JOIN field_data_field_region_for_real r
ON a.uid = r.entity_id
JOIN field_data_field_fo f
ON a.uid = f.entity_id
JOIN field_data_field_full_name fn
ON a.uid = fn.entity_id
JOIN field_data_field_type t,
ON a.uid = t.entity_id
JOIN field_data_field_title ti,
ON a.uid = ti.entity_id
JOIN field_data_field_s_code sc,
ON a.uid = sc.entity_id
JOIN field_data_field_s_group sg,
ON a.uid = sg.entity_id
JOIN field_data_field_department dn,
ON a.uid = dn.entity_id
JOIN field_data_field_region cl,
ON a.uid = cl.entity_id
JOIN field_data_field_g_office goc,
ON a.uid = goc.entity_id
JOIN field_data_field_hub hu,
ON a.uid = hu.entity_id
JOIN field_data_field_gender ge,
ON a.uid = ge.entity_id
JOIN field_data_field_full_ten ft,
ON a.uid = ft.entity_id
JOIN field_data_field_ten_since_associate tsa
ON a.uid = tsa.entity_id
where
a.uid <> 0
GROUP BY
a.pid
ORDER BY
1 DESC
One final possibility on this query under MySQL is
SELECT STRAIGHT_JOIN
f.field_fmno_value as fo ...(rest of query).
STRAIGHT_JOIN tells MySQL to do the query in the table order I provided since everything looks like a child lookup, you want the driving table to be the primary "a" table reference.
tables and query are here
http://sqlfiddle.com/#!9/2b6f35a/1/0
In it for each name i get its tags title, So it would be like this
name1: title1;
name2: title1;
name3: title1;
My problem is that i get double the tags
name1: title1, title1;
name2: title1, title1;
name3: title1;
What is the mistake i've made?
The entire problem comes for tablx
Sorry about the mess before, didn't know about sqlfiddle
The join with X table is returning the 4th row. http://sqlfiddle.com/#!9/2b6f35a/6, which draws the same title1 value in from the tags table, which is then aggregated into the final response with GROUP_CONCAT()
A solution if you need to join both these tables in one query but only want the title1 one time is to remove the GROUP_CONCAT() aggregator: http://sqlfiddle.com/#!9/2b6f35a/7
SELECT a.name, x.rate, c.title
FROM tabl1 a
LEFT JOIN tablx x ON x.pid = a.id
INNER JOIN tabl2 b ON a.id = b.pid
INNER JOIN tabl3 c ON c.id = b.bid
WHERE c.title IN ('title1')
GROUP BY a.id
In your case, it might be more useful to have an aggregator on the rate column for x table, like so: http://sqlfiddle.com/#!9/2b6f35a/9
SELECT a.name, x.rate, c.title, SUM(x.rate) AS rate_sum
FROM tabl1 a
LEFT JOIN tablx x ON x.pid = a.id
INNER JOIN tabl2 b ON a.id = b.pid
INNER JOIN tabl3 c ON c.id = b.bid
WHERE c.title IN ('title1')
GROUP BY a.id
If you just want to count the number of distinct tags in this situation, you can use COUNT(DISTINCT...). http://sqlfiddle.com/#!9/2b6f35a/15:
SELECT a.name, b.id as bid, c.title, x.id as xid, x.rate, c.title, SUM(x.rate) AS rate_sum, COUNT(DISTINCT c.title) as title_count
FROM tabl1 a
LEFT JOIN tablx x ON x.pid = a.id
INNER JOIN tabl2 b ON a.id = b.pid
INNER JOIN tabl3 c ON c.id = b.bid
WHERE c.title IN ('title1')
GROUP BY a.id
If everything in the posted question is correct, you aren't doing anything wrong.
See this sqlfiddle:
http://sqlfiddle.com/#!9/03205d/1
I have a bills table with column customer_type and customer_id fields.
This customer_type tells if the customer is in the customers table or in the users table or in the suppliers table.
I need to create a query with left join according to customer_type.
select c.* from bills b
left join ***b.customer_type*** c on c.id = b.customer_id
You could join all three with necessary condition:
select c.*, u.*, s.* from bills b
left join customers c on c.id = b.customer_id and b.customer_type = 'customers'
left join users u on u.id = b.customer_id and b.customer_type = 'users'
left join suppliers s on s.id = b.customer_id and b.customer_type = 'suppliers'
Then you can take the data that is relevant from the result.
However if there are similar columns in these 3 tables you might want to restructure the database to only store one type of information in one place.
Is it possible to INNER JOIN a MySQL query to achieve this result?
I have a table with Strategies and a table with Members. The Strategy table holds the ID of the author that corresponds to their ID in the Member table and the ID of an author that updated the existing author's work. Is it possible to grab a reference to both of these people at the same time? Something like the following, which returns no errors, but also no results...
SELECT * FROM Strategies
INNER JOIN Members AS a
INNER JOIN Members AS b
WHERE Strategies.ID='2'
AND Strategies.AuthorID = a.ID
AND Strategies.UpdateAuthorID = b.ID
Use a LEFT JOIN:
SELECT
s.*,
a.Name AS MemberName,
b.Name AS UpdatedMemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2 ;
If you want them in one column use COALESCE:
SELECT
s.*,
COALESCE(a.Name, b.Name) AS MemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
if domain is table name
I have 2 tables: Users{username, UserID} AND Prizes{UserID, prize, status}
I want to select all the users (from Users Left Join Prizes) except the users who has 'status = dead' in the Prizes table
I think you need here is INNER JOIN because you only want to search fors user having status not equal to dead.
SELECT a.*, b.*
FROM Users a
INNER JOIN Prizes b
ON a.userID = b.UserID
WHERE b.status <> 'dead'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
SELECT a.*, b.*
FROM Users a
LEFT JOIN Prizes b
ON a.userID = b.UserID
WHERE b.UserID IS NULL OR b.status <> 'dead'
Try this;
select u.*
from users as u
left join prizes as p
on u.userid = p.userid
where p.status <>'dead';
Thanks