Please find below a link to the table-structure I have set up and to the query I am running.
Link to tables and query.
The present result is that only the firstnames, lastnames and "education_finished" are showing. But all the option_id's and their related lang_values still show "NULL".
The desired result:
Any suggestions how to solve?
Below is the query that you are using:
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_data d
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_name
LEFT JOIN phpbb_profile_fields_lang fl
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
The reason why you are getting null value is because of this condition:
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_name
You are trying to join on pf_education_finished (int) field of one table and field_name (int) field of another table. Also, there are no matching values (e.g. pf_education_finished contains numbers whereas field_nameis 'education finished').
If you want the query to return something then you need to join on field_id and phpbb_profile_fields needs to have some records with matching field id, e.g.:
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_data d
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_id
LEFT JOIN phpbb_profile_fields_lang fl
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
Here's the updated SQL Fiddle.
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_lang fl
inner JOIN phpbb_profile_fields f
ON f.field_id = fl.field_id
inner JOIN phpbb_profile_fields_data d
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
This is the optional query if you want to display data from 3-4 tables but in this query names are repeats as per the count of field_id present in phpbb_profile_fields_lang.
The exact solution you are looking is, when you have the same primary key in all the tables from which you are retrieving the data.
Thank you.
Related
I am trying to write a query that uses a LEFT OUTER JOIN on three tables. I have complete the first part to join two tables but I am stuck on intergarting the third table.
What I need is the "Status" field for the NXLHR_Valid to be included in the first query.
Below are my to queries, how would I include the SECOND query into the FIRST query
FIRST QUERY
SELECT NXLHR_SequenceNo_default.SeqNo, NXLHR_SequenceNo_default.SeqHeader, NXLHR_SequenceNo_default.SeqText, NXLHR_Hist.UniqueID, NXLHR_Hist.Room, NXLHR_Hist.Status, NXLHR_Hist.Water, NXLHR_Hist.AuditBy
FROM NXLHR_SequenceNo_default
LEFT OUTER JOIN NXLHR_Hist
ON NXLHR_SequenceNo_default.SeqID = NXLHR_Hist.SeqID
AND NXLHR_Hist.UniqueID = 'NXLHR01031472477564'
WHERE NXLHR_SequenceNo_default.SeqActive = 1
ORDER BY NXLHR_SequenceNo_default.OrderID
SECOND QUERY
SELECT NXLHR_Valid.UniqueID, NXLHR_Valid.Status
FROM NXLHR_Valid
WHERE NXLHR_Valid.UniqueID = 'NXLHR01031472477564'
Any help would be great. Thank you for your time.
SELECT d.SeqNo
, d.SeqHeader
, d.SeqText
, h.UniqueID
, h.Room
, h.Status
, h.Water
, h.AuditBy
, v.Status
FROM NXLHR_SequenceNo_default d
LEFT
JOIN NXLHR_Hist h
ON h.SeqID = d.SeqID
AND h.UniqueID = 'NXLHR01031472477564'
LEFT
JOIN NXLHR_Valid v
ON v.UniqueID = h.UniqueID
WHERE d.SeqActive = 1
ORDER
BY d.OrderID
I have a query as follows:
SELECT
staff_names.staff_ID AS sid
staff_names.name AS name,
staff_names.rec_type AS rec_type,
prod_staff.specialized AS specialized,
compspec.name AS compspec_name
FROM staff_names JOIN prod_staff USING (staff_ID)
LEFT JOIN (prod_staff_compspec JOIN company_list USING (comp_ID)) compspec
USING (prod_ID, staff_ID, role_ID)
WHERE prod_staff.role_ID = 2
AND prod_staff.prod_ID = 27
AND prod_staff.asst = 'n'
AND episode IS NOT NULL
ORDER BY name
Running this as-is says there's an error near the 'compspec' alias. Removing that and changing 'compspec' to 'company_list' in the SELECT clause returns no rows, even though it should return 1 with the given values. The left join seems to be the problem, but I don't how it should be formatted.
The prod_staff table has prod_ID, staff_ID and role_ID fields. prod_staff_compspec has these and a comp_ID field. prod_staff may or may not have a matching prod_staff_compspec row, but prod_staff_compspec always has a matching company_list row.
What I want to do is retrieve a list of all staff names associated with a given role_ID and prod_ID in the prod_staff table, as well as a company name from the company_list table, if a link to such exists in the prod_staff_compspec table (only a small minority have one).
Switched to ON to define the table relations. LEFT JOIN (prod_staff_compspec JOIN company_list USING (comp_ID)) compspec is switched to 2 left join.
select a.staff_id sid, a.name, a.rec_type, b.specialized, d.name compspec_name
from staff_names a
join prod_staff b on a.staff_id = b.staff_id
left join prod_staff_compspec c on b.prod_id = c.prod_id and b.staff_id = c.staff_id and b.role_id = c.role_id
left join company_list d on c.comp_id = d.comp_id
where b.role_id = 2 and b.prod_id = 27 and b.asst = 'n' and episode is not null
order by a.name;
I try to make this query to include all products_options_name results even if there are no related rows for the selected language in the information table.
When I run the query now, only products_propoptions are shown that have a related row for selected language in the information table.
Any help is highly appreciated.
Thanks in advance,
Bas
SELECT PPO.products_options_name, I.information_title, PTL.information_id,
COUNT(PPA.products_attributes_id) as amount,
options_id FROM products_propattributes PPA
INNER JOIN products_propoptions PPO ON PPA.options_id = PPO.products_options_id
LEFT JOIN properties_to_information PTL ON PPA.options_id = PTL.option_id
LEFT JOIN information I ON PTL.information_id = I.information_id
WHERE PPO.language_id = 6 AND I.language_id = 6 AND PPA.products_id = 121
GROUP BY PPA.options_id ORDER BY PPA.products_options_sort_order
TABLE: products_propoptions
products_options_id
language_id
products_options_name
products_options_sort_order
TABLE: products_propattributes
products_attributes_id
products_id
options_id
options_values_id
options_values_price
price_prefix
products_options_sort_order
TABLE: information
information_id
information_group_id
information_title
information_description
parent_id
sort_order
visible
noindex
language_id
TABLE: properties_to_information
properties_to_information_id
option_id
information_id
Have you tried changing the INNER JOIN to a LEFT JOIN? The INNER JOIN may be causing the query to omit the resultset rows you're seeking.
This fixed it..
Included LEFT OUTER JOINS with the where conditions in the join...
Thanks..
Bas
SELECT
PPO.products_options_name,
PTL.lexicon_id,
I.information_title,
COUNT(PPA.products_attributes_id) as amount,
options_id
FROM products_propattributes PPA
LEFT OUTER JOIN products_propoptions PPO ON PPA.options_id = PPO.products_options_id AND PPO.language_id = 6
LEFT OUTER JOIN properties_to_information PTL ON PPA.options_id = PTL.option_id
LEFT OUTER JOIN information I ON PTL.lexicon_id = I.information_id AND I.language_id = 6
WHERE PPA.products_id = 121
GROUP BY PPA.options_id ORDER BY PPA.products_options_sort_order
I have a problem with joining some tables, heres my structure:
tbl_imdb:
fldID fldTitle fldImdbID
1 Moviename 0000001
tbl_genres:
fldID fldGenre
1 Action
2 Drama
tbl_genres_rel:
fldID fldMovieID fldGenreID
1 1 1
2 1 2
What I’m trying to do is a query that will find all movies that is both an action movie and drama, is this possible to do without a subquery, if so, how?
What I'm trying right now is:
SELECT tbl_imdb.*
FROM tbl_imdb
LEFT JOIN tbl_imdb_genres_rel ON ( tbl_imdb.fldID = tbl_imdb_genres_rel.fldMovieID )
LEFT JOIN tbl_imdb_genres ON ( tbl_imdb_genres_rel.fldGenreID = tbl_imdb_genres.fldID )
WHERE tbl_imdb_genres.fldGenre = 'Drama'
AND tbl_imdb_genres.fldGenre = 'Action';
But this dosnt work, however it does work if I only keep one of the two WHERE's, but thats not what I want.
Two ways to do it:
1
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel rel_action
ON tbl_imdb.fldID = rel_action.fldMovieID
INNER JOIN tbl_genres genre_action
ON rel_action.fldGenreId = genre_action.fldID
AND 'Action' = genre_action.fldGenre
INNER JOIN tbl_genres_rel rel_drama
ON tbl_imdb.fldID = rel_drama.fldMovieID
INNER JOIN tbl_genres genre_drama
ON rel_drama.fldGenreId = genre_drama.fldID
AND 'Drama' = genre_drama.fldGenre
This method is on the same path as your original solution. 2 differences:
The join should be inner, not left because you're trying to get movies that certainly have the corresponding genre entry
Since you want to find 2 different generes, you'll have to do the join with tbl_genres_rel and tbl_genres twice, once for each particular genre you're interested in.
2
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel
ON tbl_imdb.fldID = tbl_genres_rel.fldMovieID
INNER JOIN tbl_genres
ON tbl_genres_rel.fldGenreId = tbl_genres.fldID
AND tbl_genres.fldGenre IN ('Action', 'Drama')
GROUP BY tbl_imdb.fldID
HAVING COUNT(*) = 2
Again, the basic join plan is the same. Difference here is that we join to the tbl_genres_rel and tbl_genres path just once. This on itself fetches all genres for one film, and then filters for the one's you're interested in. The ones that qualify will now have 2 rows for each distinct value of tbl_imdb.fldId. The GROUP BY aggregates on that, flattening that into one row. By asserting in the HAVING clause that we have exactly 2 rows, we ensure that we keep only those rows that have both the genres.
(Note that this assumes that there is a unique constraint on tbl_genres_rel over {fldMovieID, fldGenreID}. If such a constraint is not present, you should consider adding it.)
LEFT JOIN is not applicable in your case because records should exist on both tables. And you need to count the instances of the movie
SELECT *
FROM tbl_imdb a
INNER JOIN tbl_genres_rel b
on a.fldID = fldMovieID
INNER JOIN tbl_genres c
on c.fldGenreID = b.fldID
WHERE c.fldGenre IN ('Drama', 'Action')
GROUP BY a.Moviename
HAVING COUNT(*) > 1
I have a mysql db query that I'd like to take and condense into a single row. The output is currently two rows and will only ever be two rows. The reason I am getting two rows in the first place is because there will always be a primary and a secondary phone number associated with my result.
Since all of the data is exactly the same for both rows with the exception of the phone type (ptype) and second phone number, I'd like to just take those two columns and append them to the forst row.
I know this can be done but I can't remember how to do it.
Here is the code I am using:
SELECT
c.name,
c.address1,
p.ptype,
p.phone
FROM
customer AS c
Inner Join customeremp AS e ON c.customer_id = e.customer_id
Inner Join customerphone AS p ON e.customer_seq = p.customer_seq
WHERE
c.customer_id = '1'
You need to create 2 joins on the phone table
SELECTc.name,
c.address1,
p.ptype,
p.phone,
p2.ptype,
p2.phone,
FROMcustomer AS cInner
Join customeremp AS e ON c.customer_id = e.customer_idInner
Join customerphone AS p ON e.customer_seq = p.customer_seq and p.pType = 1 (or whatever private means)
Join customerphone AS p2 ON e.customer_seq = p2.customer_seq and p2.pType = 2 (or whatever the other type is)
WHEREc.customer_id = '1'
Edit: this query will only return customer which have BOTH phone types set. If they are optional, then you should consider changing the join into an outer join
select c.name,c.address1,primary.phone,secondary.phone
from customer c
join customeremp e
on c.customer_id = e.customer_id
join customerphone primary
on primary.ptype = 'primary'
and e.customer_seq = primary.customer_seq
join customerphone secondary
on secondary.ptype = 'secondary'
and e.customer_seq = secondary.customer_seq
where c.customer_id = '1'
Try this:
SELECT
c.name,
c.address1,
IF(p.ptype = 'primary', p.phone, null)),
IF(p.ptype = 'secondary', p.phone, null))
FROM customer JOIN (blah blah)
WHERE customer_id = 1
GROUP BY c.name, c.address1
If it doesn't work, you might need to put an aggregate function on the last two fields, eg: MAX(IF(p.ptype ...