I'm trying to do the following MySql query and when i go to run it, it tells me that "q.object_id" in the sub-query is unknown (if i change it to o.id, it says the same).
SELECT q.*, (SELECT c.title
FROM api_course c
LEFT OUTER JOIN api_object_parents op
ON c.object_id = op.parent
AND op.object_id = q.object_id) as parent_title
FROM api_quiz q
LEFT OUTER JOIN api_object o ON q.object_id = o.id
WHERE o.type = 'Quiz'
Basically i need to get the same id that is being used in the main query and use that in a sub-query.
Thanks!
is this what you are looking for?
SELECT a.*, d.title
FROM api_quiz a
LEFT JOIN api_object b
on a.object_id = b.id
LEFT JOIN api_object_parents c
ON c.parent = a.object_id
LEFT JOIN api_course d
ON c.object_id = c.parent
Related
I have been working on a multi-table query (something I haven't had much experience in) and at first I thought it was working perfectly fine until I noticed that half of the results had null values. I have put the query and table structures below so any help would be appreciated!
SELECT
i.name, i.material, i.price, a.str_mod, a.def_mod,
a.dex_mod, a.spd_mod, i.level_req
FROM `character` as c
LEFT JOIN item_owned as o ON c.uid = o.oid
LEFT JOIN items as i ON o.iid = i.id
LEFT JOIN armour as a ON i.id = a.aid
WHERE uid = :id AND o.equipped = 1 AND i.type = 'Armour'
Above is the query I have been running and below is the table structures
Found the solution thanks to Malfunct on discord... The query had a column typo so should have been
SELECT
i.name, i.material, i.price, a.str_mod, a.def_mod, a.dex_mod, a.spd_mod, i.level_req
FROM `character` as c
JOIN item_owned as o ON c.uid = o.oid
JOIN items as i ON o.iid = i.id
JOIN armour as a ON i.id = a.aid WHERE uid = 1
AND o.equipped = 1
AND i.type = 'Armour'
We have a home grown document management system and our system is running very slow, particularly on the search. It worked fine at first, but it has gotten progressively slower over time. Its now taking anywhere from 30 to 150 seconds to return results depending upon criteria. This is our search query. We’ve been staring at this thing left and right and can’t see any place to tune this more. All of the joined fields are indexed on their respective tables.
SELECT DISTINCT f.*, ts.*, fo.*, ft.*, p.*, u.*, c.*, co.*, ct.*, fs.*, fd.*, r.*, rt.*, si.*, s.* FROM (
SELECT DISTINCT f.* FROM files f
JOIN folders fo ON(fo.id = f.belongs_to_folder_id)
JOIN projects p ON(p.id = f.belongs_to_project_id)
LEFT OUTER JOIN file_statuses fs ON(fs.id = f.file_status_id)
LEFT OUTER JOIN submittal_items_files sif ON(sif.file_id = f.id)
LEFT OUTER JOIN submittal_items si ON(si.id = sif.submittal_item_id)
LEFT OUTER JOIN submittals s ON(s.id = si.belongs_to_submittal_id)
LEFT OUTER JOIN record_types rt ON(rt.id = f.record_type_id)
LEFT OUTER JOIN companies co ON(co.id = f.company_id)
LEFT JOIN folders_actions_groups ag ON (
f.belongs_to_folder_id = ag.folder_id AND
ag.action_id = 10010
)
LEFT JOIN files_actions_groups fg ON (fg.file_id = f.id)
JOIN users_groups ug ON ((ug.group_id = ag.group_id OR ug.group_id = fg.group_id) AND ug.user_id = 411)
WHERE (
(f.file_generated_name LIKE CONCAT('%', 'the', '%')) OR
(f.record_id LIKE CONCAT('%', 'the', '%')) OR
(f.record_title LIKE CONCAT('%', 'the', '%')) OR
(f.additional_info LIKE CONCAT('%', 'the', '%')) OR
(si.item_number LIKE CONCAT('%', 'the', '%')) OR
(s.element_number LIKE CONCAT('%', 'the', '%'))
) AND f.path LIKE CONCAT('Some Text', '%') AND
f.file_status_id = 3 AND
f.file_revision = 1 AND
f.discipline_id = 1 AND
f.record_type_id = 2 AND
f.triage_status_id = 2 AND
f.deleted = 0
ORDER BY f.created DESC, f.id DESC
LIMIT 100
) AS f
LEFT OUTER JOIN users u ON(f.created_by_user_id = u.id)
LEFT OUTER JOIN contacts c ON(c.user_id = u.id)
LEFT OUTER JOIN companies co ON(co.id = f.company_id)
LEFT OUTER JOIN company_types ct ON(ct.id = co.company_type_id)
JOIN triage_statuses ts ON(f.triage_status_id = ts.id)
JOIN folders fo ON(fo.id = f.belongs_to_folder_id)
JOIN folder_types ft ON(ft.id = fo.folder_type_id)
JOIN projects p ON(p.id = f.belongs_to_project_id)
LEFT OUTER JOIN file_statuses fs ON(fs.id = f.file_status_id)
LEFT OUTER JOIN file_disciplines fd ON(fd.id = f.discipline_id)
LEFT OUTER JOIN revisions r ON(r.id = f.file_revision)
LEFT OUTER JOIN record_types rt ON(rt.id = f.record_type_id)
LEFT OUTER JOIN submittal_items_files sif ON(sif.file_id = f.id)
LEFT OUTER JOIN submittal_items si ON(si.id = sif.submittal_item_id)
LEFT OUTER JOIN submittals s ON(s.id = si.belongs_to_submittal_id)
LEFT OUTER JOIN files_actions_groups ffg ON(ffg.file_id = f.id)
LEFT OUTER JOIN groups g ON(g.id = ffg.group_id)
ORDER BY f.created DESC, f.id DESC
This might be an obvious answer, but have you indexed your database? If you're new to indexing, here's a pretty good rule: just put a unique index on all the columns named "id", such as folders.id or projects.id, then put a standard index on all the columns that reference a foreign id, such as folder.belongs_to_folder_id or folder.record_type_id
Another thing I would change is to try and select only the columns you will actually use rather than your huge list of f.*, ts.*, fo.*, ft.*, p.*, u.*, c.*, co.*, ct.*, fs.*, etc...
You also have TONS of joins, which are very expensive in terms of processing time. Do you really need all those joined tables?
I have a mysql query as stated below, it returns exactly the results I want for one row, but doesn't return any other rows where I expect there to be 8 in my test data (there are 8 unique test ids). I was inspired by this answer but obviously messed up my implementation, does anyone see where I'm going wrong?
SELECT
c.first_name,
c.last_name,
n.test_name,
e.doc_name,
e.email,
e.lab_id,
a.test_id,
a.date_req,
a.date_approved,
a.accepts_terms,
a.res_value,
a.reason,
a.test_type,
a.date_collected,
a.date_received,
k.kind_name,
sum(case when metabolite_name = "Creatinine" then t.res_val end) as Creatinine,
sum(case when metabolite_name = "Glucose" then t.res_val end) as Glucose,
sum(case when metabolite_name = "pH" then t.res_val end) as pH
FROM test_requisitions AS a
INNER JOIN personal_info AS c ON (a.user_id = c.user_id)
INNER JOIN test_types AS d ON (a.test_type = d.test_type)
INNER JOIN kinds AS k ON (k.id = d.kind_id)
INNER JOIN test_names AS n ON (d.name_id = n.id)
INNER JOIN docs AS e ON (a.doc_id = e.id)
INNER JOIN test_metabolites AS t ON (t.test_id = a.test_id)
RIGHT JOIN metabolites AS m ON (m.id = t.metabolite_id)
GROUP BY a.test_id
ORDER BY (a.date_approved IS NOT NULL),(a.res_value IS NOT NULL), a.date_req, c.last_name ASC;
Most of your joins are inner joins. The last is a right outer join. As written, the query keeps all the metabolites, but not necessarily all the tests.
I would suggest that you change them all to left outer joins, because you want to keep all the rows in the first table:
FROM test_requisitions AS a
LEFT JOIN personal_info AS c ON (a.user_id = c.user_id)
LEFT JOIN test_types AS d ON (a.test_type = d.test_type)
LEFT JOIN kinds AS k ON (k.id = d.kind_id)
LEFT JOIN test_names AS n ON (d.name_id = n.id)
LEFT JOIN docs AS e ON (a.doc_id = e.id)
LEFT JOIN test_metabolites AS t ON (t.test_id = a.test_id)
LEFT JOIN metabolites AS m ON (m.id = t.metabolite_id)
I would also suggest that your aliases be related to the table, so tr for test_requisition, pi for personal_info, and so on.
Here's a picture of my database structure:
With help from users on here I've managed to put together quite a complex SQL statement using GROUP_CONCAT:
SELECT
t1.Name AS Teacher_Name,
t2.Name AS Observer_Name,
o.Datetime AS Datetime,
o.Type AS Type,
o.Year_Group AS Year_Group,
o.Class_Name AS Class_Name,
c.Title AS Course_Name,
GROUP_CONCAT(l.Title) AS Focus,
o.Achievement_Grade AS Achievement_Grade,
o.Behaviour_Grade AS Behaviour_Grade,
o.Teaching_Grade AS Teaching_Grade,
GROUP_CONCAT(cl1.Title) AS Positive,
GROUP_CONCAT(cl2.title) AS Development,
o.Notes AS Notes
FROM observations o
LEFT JOIN teachers t1
ON o.Teacher_ID = t1.Teacher_ID
LEFT JOIN teachers t2
ON o.Observer_ID = t2.Teacher_ID
LEFT JOIN courses c
ON o.Course_ID = c.Course_ID
LEFT JOIN foci f
ON o.ID = f.Observation_ID
LEFT JOIN focus_labels l
on f.focus_id = l.id
LEFT JOIN criteria c1
ON o.ID = c1.Observation_ID
LEFT JOIN criteria_labels cl1
on c1.Criteria_ID = cl1.ID AND c1.Type = 'P'
LEFT JOIN criteria c2
ON o.ID = c2.Observation_ID AND c2.Type = 'D'
LEFT JOIN criteria_labels cl2
on c2.Criteria_ID = cl2.ID
GROUP BY o.id
ORDER BY `Datetime` DESC";
This appears to work OK, apart from the fact that Focus, Positive and Development are each repeated depending on the field that has the highest number of concatenations in.
For example, if Positive has Pace,Progress,Attainment but Focus is only Appraisal, it'll be repeated three times (Appraisal,Appraisal,Appraisal).
I've looked this up and I think it could be because I need to GROUP each of these GROUP_CONCAT JOINs. However, I have no idea how to go about this.
Can anyone help? Thanks in advance,
GROUP_CONCAT has DISTINCT attribute that can be applied to remove duplicates.
SELECT
t1.Name AS Teacher_Name,
t2.Name AS Observer_Name,
o.Datetime AS Datetime,
o.Type AS Type,
o.Year_Group AS Year_Group,
o.Class_Name AS Class_Name,
c.Title AS Course_Name,
GROUP_CONCAT(DISTINCT l.Title) AS Focus,
o.Achievement_Grade AS Achievement_Grade,
o.Behaviour_Grade AS Behaviour_Grade,
o.Teaching_Grade AS Teaching_Grade,
GROUP_CONCAT(cl1.Title) AS Positive,
GROUP_CONCAT(cl2.title) AS Development,
o.Notes AS Notes
FROM observations o
LEFT JOIN teachers t1
ON o.Teacher_ID = t1.Teacher_ID
LEFT JOIN teachers t2
ON o.Observer_ID = t2.Teacher_ID
LEFT JOIN courses c
ON o.Course_ID = c.Course_ID
LEFT JOIN foci f
ON o.ID = f.Observation_ID
LEFT JOIN focus_labels l
on f.focus_id = l.id
LEFT JOIN criteria c1
ON o.ID = c1.Observation_ID
LEFT JOIN criteria_labels cl1
on c1.Criteria_ID = cl1.ID AND c1.Type = 'P'
LEFT JOIN criteria c2
ON o.ID = c2.Observation_ID AND c2.Type = 'D'
LEFT JOIN criteria_labels cl2
on c2.Criteria_ID = cl2.ID
GROUP BY o.id
ORDER BY `Datetime` DESC";
SELECT p.id
FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty
INNER JOIN sub_subkategorie ssi
ON pr.sub_subkategorie_id = ssi.ID
Tables and their important fields
produkty - id, pozycja
przyporzadkowania - id, produkt_id, sub_kategoria_id, sub_subkategoria_id
sub_subkategorie - id, subkategorie_id, pozycja
subkategorie - id, kategorie_id, pozycja
kategorie - id, pozycja
Error "#1054 - Unknown column 'pr.sub_subkategorie_id' in 'on clause'"
Tried with
SELECT p.id, pr.sub_subkategorie_id
Same result.
Full query (not tested due to failure of query above):
SELECT p.id
FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty
INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
INNER JOIN subkategorie si ON ssi.subkategorie_id = si.id
INNER JOIN kategorie c ON si.kategorie_id = c.id
WHERE stany_magazynowe.produkty_id = p.id
AND p.id = pr.produkty_id
AND pr.sub_subkategorie_id =1
AND p.widoczny = '1'
AND p.id = gk_grupy_produkty.id_produktu
AND gk_grupy_produkty.id_grupy =1
AND gk_grupy_produkty.towar_widocznosc =1
AND c.id = '1'
ORDER BY c.pozycja, si.pozycja, ssi.pozycja, p.pozycja
Hope that I gave enough info (earlier question - SELECT * FROM table WHERE field IN (SELECT id FROM table ORDER BY field2))
EDIT:
Yes, there is typo, but only here, on stackoverflow (too much coffee, my fingers are flying). Thank you all, You saved my day!
You're joining the tables in the wrong order:
SELECT p.id
FROM produkty p, stany_magazynowe, gk_grupy_produkty, przyporzadkowania pr
INNER JOIN sub_subkategorie ssi
ON pr.sub_subkategorie_id = ssi.ID
The error is due to the higher precedence of the JOIN keyword compared to the comma. Errors like this are one of the reasons why I would urge you not to use the implicit join syntax with the comma and instead always write your joins explicitly using the JOIN keyword.
Here is your complete query rewritten using explicit joins:
SELECT p.id
FROM produkty p
INNER JOIN przyporzadkowania pr ON p.id = pr.produkty_id
INNER JOIN stany_magazynowe ON stany_magazynowe.produkty_id = p.id
INNER JOIN gk_grupy_produkty ON p.id = gk_grupy_produkty.id_produktu
INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
INNER JOIN subkategorie si ON ssi.subkategorie_id = si.id
INNER JOIN kategorie c ON si.kategorie_id = c.id
WHERE pr.sub_subkategorie_id = 1
AND p.widoczny = '1'
AND gk_grupy_produkty.id_grupy =1
AND gk_grupy_produkty.towar_widocznosc =1
AND c.id = '1'
ORDER BY c.pozycja, si.pozycja, ssi.pozycja, p.pozycja
Related question
Mixing implicit and explicit JOINs
You are mixing "classical" joins with the join keyword. You should use the join keyword for all the joins.
The error comes from that you are joining on the gk_grupy_produkty table, where that field doesn't exist. The database really looks at your query as:
SELECT p.id
FROM
produkty p,
przyporzadkowania pr,
stany_magazynowe,
(gk_grupy_produkty INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID)
You should use:
SELECT p.id
FROM
produkty p
INNER JOIN przyporzadkowania pr ON p.id = pr.produkty_id
INNER JOIN stany_magazynowe ON stany_magazynowe.produkty_id = p.id
INNER JOIN gk_grupy_produkty ON p.id = gk_grupy_produkty.id_produktu
INNER JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
INNER JOIN subkategorie si ON ssi.subkategorie_id = si.id
INNER JOIN kategorie c ON si.kategorie_id = c.id
WHERE
pr.sub_subkategorie_id = 1 AND
p.widoczny = '1' AND
gk_grupy_produkty.id_grupy = 1 AND
gk_grupy_produkty.towar_widocznosc = 1 AND
c.id = '1'
ORDER BY c.pozycja, si.pozycja, ssi.pozycja, p.pozycja
Is the column named sub_subkategoria_id a typo?
Start with this: (fill in the ???)
SELECT p.id
FROM produkty p
JOIN przyporzadkowania pr ON p.??? = pr.???
JOIN sub_subkategorie ssi ON pr.sub_subkategorie_id = ssi.ID
(You don't need to specify INNER)
Your original FROM clause:
FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty
Has tables p and pr for which no JOIN condition is specified which will result in a cartesian product or cross join (probably not a good thing in this case) as well as tables that aren't referenced anywhere else in your query:
stany_magazynowe, gk_grupy_produkty
Specify a JOIN for
produkty p AND przyporzadkowania pr
and remove
stany_magazynowe, gk_grupy_produkty
Hint: If you do not reference any columns from a table in your SELECT, ORDER BY, GROUP BY, WHERE then the table probably does not belong in your FROM clause. (Unless it's a join/junction table.)