JOINING a query from 2 tables - Multiple Results - mysql

Could someone please tell explain to me how to properly process a query that collects information from 2 tables, i thought I had this figured out until I added more records. Please look at the image I have below:enter image description here
(The last record should not have the name "Thomas Murray" in it)
Then there is the query I am processing:
"select a.*, b.forenames, b.surname FROM playerSkills a, playerdb b GROUP BY sheetNo"
What I was hoping to do is collect all from the playerSkills database (which it does) and only bring over the names from the second database (playerdb) that matched with the playerID but as I want to return more than one result so I don't know what to do as it returns the whole column and just pastes the one name into every field.
Though I am sure a JOIN is to be inserted here, I am not sure which or at all.
I am not experienced with SQL but trying to wrap my head around it. I have experimented with the JOIN clauses but didn't get far probably due to a syntax.
How can join the names to the playerID so they appear in the appropriate fields?

You need columns to join on . . . and proper join syntax:
select ps.*, p.forenames, p.surname
FROM playerSkills ps JOIN
playerdb p
ON ps.playerId = p.playerId;
Notes:
Your query does not require GROUP BY.
Your query does require JOIN conditions.
Kudos for using table aliases. They should be abbreviations for the table name.
You want to always use explicit JOIN syntax. No commas in the FROM clause.

Related

Why Can My Database Not See This Database Column?

I have a table called "customers". It contains a column called "id" and for some amazing reason, I am getting the error:
#1054 - Unknown column 'c.id' in 'on clause'
When I do this join:
SELECT c.co_name, p.name
FROM `customers` as c, `products` as p, `customer_product` as cp
Join `products`
On c.id = cp.customer_id
I have also tried removing the alias and writing out customers.id and I still get the error.
Why am I getting the error - what have I done wrong ?
Many Thanks !
It is probably failing as you are using alias and full table.. pick one... Also, you will get a Cartesian result as you don't have anything tying between cp and p tables.. POSSIBLE solution.
SELECT
c.co_name,
p.name
FROM
customers c
JOIN customer_product cp
on c.id = cp.customer_id
Join products p
on cp.product_id = p.id
Clarification per request.
I have a style I like to use for doing my SQL-based queries. If you click on my image of the answer (or anyone for that matter), it will take you to their profile / history. Then if you click on the MySQL tag, it will take you to a list of all answers I've provided for MySQL (or whatever tag you are interested in from me or any other user). From that, look at the pattern of styling of my answers.
I like to use indentation as a visual representation of how table "A" correlates to "B" and from "B" to "C" as can be seen here. You used a cross between comma separated tables with no defined JOIN and then a join only on the third table. This means only the JOIN condition was applied.
When I did the query, I listed the primary table first, JOIN to the second and immediately identify the ON condition between the FIRST (left-side) table and the SECOND (right-side) table. Since you have a link-table (identifying customers and products), the first criteria joined from the customer table to the link table by the customer. So now, you need to get from the link table to the product table. So now, the indentation moves the product table further in and now the FIRST (left-side) table is the LINK (customer_products) table joined to the SECOND (right-side) table of products. Now the ON condition between those.
So now, relations are established between A-B and B-C.
Once this is done, then you can get whatever columns to be returned and THEN apply any other criteria / group by / order by conditions. Note, If I had additional criteria at one of the secondary (or deeper) levels, I would add the criteria exactly at that location. This way, when dealing with left-join conditions, I am not accidentally turning it to an INNER join by putting the criteria in the WHERE clause.
Hope this and some other answers that you can look at help in your future querying.

Using an INNER JOIN without returning any columns from the joined table

Running an INNER JOIN type of query, i get duplicate column names, which can pose a problem. This has been covered here extensively and i was able to find the solution to this problem, asides from it being fairly logical, by SELECTing only the columns i need.
However, i would like to know how i could run such a query without actually returning any of the columns from the joined table.
This is my MySQL query
SELECT * FROM product z
INNER JOIN crosslink__productXmanufacturer a
ON z.id = a.productId
WHERE
(z.title LIKE "%search_term%" OR z.search_keywords LIKE "%search_term%")
AND
z.availability = 1
AND
a.manufacturerId IN (22,23,24)
Question
How would i modify this MySQL query in order to return only columns from product and none of the columns from crosslink__productXmanufacturer?
Add the table name to the *. Replace
SELECT * FROM product z
with
SELECT z.* FROM product z
Often when you are doing this, the intention may be clearer using in or exists rather than a join. The join is being used for filtering, so putting the condition in the where clause makes sense:
SELECT p.*
FROM product p
WHERE (p.title LIKE '%search_term%' OR p.search_keywords LIKE '%search_term%') AND
p.availability = 1 AND
exists (SELECT 1
FROM pXm
WHERE pXm.productId = p.id AND pxm.manufacturerId IN (22, 23, 24)
);
With the proper indexes, this should run at least as fast as the join version (the index is crosslink__productXmanufacturer(productId, manufacturerId). In addition, you don't have to worry about returning duplicate records, if there are multiple matches in crosslink__productXmanufacturer.
You may notice two other small changes I made to the query. First, the table aliases are abbreviates for the table names, making the logic easier to follow. Second, the string constants use single quotes (the ANSI standard) rather than double quotes. Using single quotes only for string and date constants helps prevent inadvertent syntax errors.

How to combine 5 tables together with same ID in a query?

I have 5 different tables T_DONOR, T_RECIPIENT_1, T_RECIPIENT_2, T_RECIPIENT_3, and T_RECIPIENT_4. All 5 tables have the same CONTACT_ID.
This is the T_DONOR table:
T_RECIPIENT_1:
T_RECIPIENT_2:
This is what I want the final table to look like with more recipients and their information to the right.
T_RECIPIENT_3 and T_RECIPIENT_4 are the same as T_RECIPIENT_1 and T_RECIPIENT_2 except that they have different RECIPIENT ID and different names. I want to combine all 5 of these tables so on one line I can have the DONOR_CONTACT_ID which his information, and then all of the Recipient's information.
The problem is that when I try to run a query, it does not work because not all of the Donors have all of the recipient fields filled, so the query will run and give a blank table. Some instances I have a Donor with 4 Recipients and other times I have a Donor with only 1 Recipient so this causes a problem. I've tried running queries where I connect them with the DONOR_CONTACT_ID but this will only work if all of the RECIPIENT fields are filled. Any suggestions on what to do? Is there a way I could manipulate this in VBA? I only know some VBA, I'm not an expert.
First I think you want all rows from T_DONOR. And then you want to pull in information from the recipient tables when they include DONOR_CONTACT_ID matches. If that is correct, LEFT JOIN T_DONOR to the other tables.
Start with a simpler set of fields; you can add in the "name" fields after you get the joins set to correctly return the rest of the data you need.
SELECT
d.DONOR_CONTACT_ID,
r1.RECIPIENT_1,
r2.RECIPIENT_1
FROM
(T_DONOR AS d
LEFT JOIN T_RECIPIENT_1 AS r1
ON d.ORDER_NUMBER = r1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2 AS r2
ON d.ORDER_NUMBER = r2.ORDER_NUMBER;
Notice the parentheses in the FROM clause. The db engine requires them for any query which includes more than one join. If possible, set up your joins in Design View of the query designer. The query designer knows how to add parentheses to keep the db engine happy.
Here is a version without aliased table names in case it's easier to understand and set up in the query designer ...
SELECT
T_DONOR.DONOR_CONTACT_ID,
T_RECIPIENT_1.RECIPIENT_1,
T_RECIPIENT_2.RECIPIENT_1
FROM
(T_DONOR
LEFT JOIN T_RECIPIENT_1
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_2.ORDER_NUMBER;
SELECT T_DONOR.ORDER_NUMBER, T_DONOR.DONOR_CONTACT_ID, T_DONOR.FIRST_NAME, T_DONOR.LAST_NAME, T_RECIPIENT_1.RECIPIENT_1, T_RECIPIENT_1.FIRST_NAME, T_RECIPIENT_1.LASTNAME
FROM T_DONOR
JOIN T_RECIPIENT_1
ON T_DONOR.DONOR_CONTACT_ID = T_RECIPIENT_1.DONOR_CONTACT_ID
This shows you how to JOIN the first recipient table, you should be able to follow the same structure for the other three...

Need help querying unused id's in relational table

I'm trying to query my database for unused listId's but can't quite seem to get the query syntax correct. Here is what I've tried:
SELECT DISTINCT pkListId FROM tblList
INNER JOIN InUseLists ON
tblList.pkListId NOT LIKE InUseLists.fkListId
Where tblList holds all List data, and InUseLists is a view holding all distinct listId's from my relational table.
Currently my tblList holds listId 1-9
my relational table has fkListId 1,8,9 (used more than once)
So my view gets distinct values from the relational table - 1,8,9.
What is wrong with my query syntax?
It sounds like you want a left join (which can succeed whether or not it finds a match in the "right" table), and then to select those rows where the join, in fact, failed to work.
Something like:
SELECT DISTINCT pkListId FROM tblList
LEFT JOIN InUseLists ON
tblList.pkListId LIKE InUseLists.fkListId
WHERE InUseLists.fkListId is null
(If LIKE is the right operator here. Most joins use a simple equality check)
If this still isn't right, it might be better if you edited your question to include the sample data and expected results in a tabular form. At the moment, I'm still not sure what the contents of your tables are, and what you're aiming for.
Try:
SELECT DISTINCT tblList.pkListId, FROM tblList, InUseLists WHERE
tblList.pkListId NOT IN(InUseLists.fkListId)

MySQL JOIN based on dynamic LIKE statement between multiple tables

I have a table called faq. This table consists from fields faq_id,faq_subject.
I have another table called article which consists of article_id,ticket_id,a_body and which stores articles in a specific ticket. Naturally there is also a table "ticket" with fields ticket_id,ticket_number.
I want to retrieve a result table in format:
ticket_number,faq_id,faq_subject.
In order to do this I need to search for faq_id in the article.a_body field using %LIKE% statement.
My question is, how can I do this dynamically such that I return with SQL one result table, which is in format ticket_number,faq_id,faq_subject.
I tried multiple configurations of UNION ALL, LEFT JOIN, LEFT OUTER JOIN statements, but they all return either too many rows, or have different problems.
Is this even possible with MySQL, and is it possible to write an SQL statement which includes #variables and can take care of this?
First off, that kind of a design is problematic. You have certain data embedded within another column, which is going to cause logic as well as performance problems (since you can't index the a_body in such a way that it will help the JOIN). If this is a one-time thing then that's one issue, but otherwise you're going to have problems with this design.
Second, consider this example: You're searching for faq_id #123. You have an article that includes faq_id 4123. You're going to end up with a false match there. You can embed the faq_id values in the text with some sort of mark-up (for example, [faq_id:123]), but at that point you might as well be saving them off in another table as well.
The following query should work (I think that MySQL supports CAST, if not then you might need to adjust that).
SELECT
T.ticket_number,
F.faq_id,
F.faq_subject
FROM
Articles A
INNER JOIN FAQs F ON
A.a_body LIKE CONCAT('%', F.faq_id, '%')
INNER JOIN Tickets T ON
T.ticket_id = A.ticket_id
EDIT: Corrected to use CONCAT
SELECT DISTINCT t.ticket_number, f.faq_id, f.faq_subject
FROM faq.f
INNER JOIN article a ON (a.a_body RLIKE CONCAT('faq_id: ',faq_id))
INNER JOIN ticket t ON (t.ticket_id = a.ticket_id)
WHERE somecriteria