I have a table in MySql Db named as newtbl which has 2 columns referenced as a foreign keys they are m_id, p_id. I want that instead of these m_id and p_id their names should be displayed.
Query which I wrote is below, but it has errors.
select newtbl.*, pa.p_name, m.m_id
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
You can try to column an alias name used as
select newtbl.*, pa.p_name, m.m_id as 'mmID'
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
Below is a answer for my question.
select newtbl.u_id, pa.p_name from newtbl left join players pa on newtbl.p_id = pa.p_id
Related
I have a database structure with the following setup:
po: id, stockNumber, factoryId, other columns
order: id, stockNumber, factoryId, other columns
stock_number: id, stockNumber, groupId
factory: id, name, groupId
The important part here is the stock_number/factory tables. The groupId column is just an integer and if two or more rows in the table have the same value then their stock numbers/factory are considered the same. Typically this is used for different sizes of the same product.
What I'd like to do is write a query that will join "order" to "po" through the group of stock_number and factory so I can find orders with no matching po. Also the factory has to match the same way.
I have this query if I have a specific stock number/factory in mind but I'd like to update it to query the whole orders table for me:
SELECT id
FROM order
WHERE
styleNumber IN (SELECT a.stockNumber FROM stock_number a INNER JOIN stock_number b ON a.groupId = b.groupId or a.id = b.id WHERE b.stockNumber = '123')
AND factoryId IN (SELECT a.submitter_id FROM submitter a INNER JOIN submitter b ON a.groupId = b.groupId OR a.submitter_id = b.submitter_id WHERE b.SUBMITTER_ID = 'alpha');
EDIT: I came up with this query which I think might be on the right track. It only joins in the stock number so it doesn't do factory yet. Can anyone confirm if I'm going in the correct direction:
SELECT *
FROM order o
LEFT JOIN stock_number s_o ON o.stockNumber = s_o.stockNumber
LEFT JOIN stock_number s_p ON s_o.groupId = s_p.groupId
LEFT JOIN po p ON s_p.stockNumber = p.stockNumber
WHERE p.id IS NULL;
Just join all the tables.
select o.id
FROM order AS o
JOIN stock_number AS sn ON sn.stockNumber = o.stockNumber
JOIN submitter AS su ON ON o.factoryId = su.submitter_id
You could use an anti-join pattern. In this example, it looks complicated because of the two relationship tables. But a query something like this:
SELECT o.id
, o.stockNumber
, o.factoryId
FROM `order` o
LEFT
JOIN `stock_number` s
ON s.stockNumber = o.stockNumber
LEFT
JOIN `factory` f
ON f.id = o.factoryId
AND f.groupId = s.groupId
LEFT
JOIN `po` p
ON p.stockNumber = s.stockNumber
AND p.factoryId = f.id
WHERE p.id IS NULL
The anti-join pattern is easier to visualize with a simpler example. Say you had the order table (as in your example), and an order_line table, with rows related to the order table by the order_id column.
order_line: id, order_id, othercolumns
To get order along with matching order_line rows:
SELECT o.id AS order_id
, l.id AS line_id
FROM `order` o
JOIN `order_line` l
ON l.order_id = o.id
To include rows from order that don't have any matching rows in order_line, we can use an outer join. We add the LEFT keyword:
SELECT o.id AS order_id
, l.id AS line_id
FROM `order` o
LEFT
JOIN `order_line` l
ON l.order_id = o.id
That gets all rows from order, including rows that don't have a matching row in order_line. The trick now is to exclude all the rows that have a matching row. For any rows that didn't have a match, the columns from order_line will be NULL. So we can add a test in the WHERE clause, to exclude rows that had a match.
SELECT o.id AS order_id
, l.id AS line_id
FROM `order` o
LEFT
JOIN `order_line` l
ON l.order_id = o.id
WHERE l.order_id IS NULL
That gets us rows from order that don't have a matching row in order_line.
We can use this same pattern in a more complicated query. We use outer join operations, and rows from order that don't have a matching row in po will have NULL values for the columns from po.
I have a main table:
- cam (id, name, type, desc, tenant_id)
with information normalized into 3 different tables:
- cs with columns (cam_id, st_id)
- ot with columns (cam_id, loc_id, tar_id, tenant_id)
- st with columns (id, code, name)
please note:
- cs.st_id is a foreign key to st.id
- ot.loc_id is a foreign key to st.id,
- ot.tar_id is a foreign key to st.id
My intention is to get the
- st.code value for all ot.loc_id and cs.st_id
(I am not interested in the id's but the their codes which is stored in table st)
This SQL:
- select
cam.id, cam.name, camp.type, cam.desc, st.code as cs.code
from
cam
left join cs on cam.id = cs.cam_id
left join ot on cam.id = ot.cam_id
left join st on cam.tenant_id = st.tenant_id;
works in that the last join condition to st table makes the st.codes available.
But what do I have to do to get the ot.loc_id codes?? I can't have multiple from clauses right? or multiple tables in from clause ... right?
Or is there no way out but to make separate SQL statements (which may not be performant i.e making an additional call)?
Thanks!
Take-Away: The join condition does not need to include the table in the from clause! Please see answer below.
No you can't have multiple from clauses
Yes you can have multiple tables in from clause
For example you could do :
select a.id, a.name, a.type, a.desc, b.code as cs.code, b.code
from cam a, st b, cs c, ot d
where a.id = c.cam_id
and a.id = d.cam_id
and st.id = d.loc_id
and b.tenant_id = a.tenant_id
Or your could simply refer to field in joined tables or views in the select statement.
When you do a join, you "join" the table to your select statement and can access them.
Example with join :
select cam.id, cam.name, camp.type, cam.desc, st.code as cs.code, st.code
from cam
left join cs on cam.id = cs.cam_id
left join ot on cam.id = ot.cam_id
left join st on cam.tenant_id = ot.tenant_id
left join st st2 on st.id = ot.loc_id;
Is your intention to join to the st table twice?
select cam.id, cam.name, camp.type, cam.desc, st.code as cs_code , st.code as loc_code
from cam left join
cs
on cam.id = cs.cam_id left join
ot
on cam.id = ot.cam_id left join
st
on cam.tenant_id = st.id left join
st stloc
on ot.loc_id = stloc.id;
According to the columns in your tables, st doesn't have a tenant_id so I changed that to just id.
I got a problem when I was trying to join 3 tables.
Below are the mysql syntax i was using :
SELECT
FROM carecell
LEFT JOIN staff ON cc.id_pks = staff.id_emp
LEFT JOIN staff ON cc.id_wpks = staff.id_emp
INNER JOIN member ON member.id_member = staff.id_member
Please Help me.. What should I do in order to fix the syntax?
The SQL engine cannot distinguish between the two staff tables in the from clause. You need to give them different names. Something like this:
FROM carecell cc LEFT JOIN
staff s1
ON cc.id_pks = s1.id_emp LEFT JOIN
staff s2
ON cc.id_wpks = s2 .id_emp INNER JOIN
member m
ON m.id_member = s2.id_member
If you join the same table multiple times you need to give that table each time a different alias name so the DB engine can differ between them
SELECT *
FROM carecell as cc
LEFT JOIN staff as s1 ON cc.id_pks = s1.id_emp
LEFT JOIN staff as s2 ON cc.id_wpks = s2.id_emp
INNER JOIN member as m ON m.id_member = s1.id_member
You already used aliases since you gave carecell the alias cc. In the last line of your query I joined s1 but you have to decide which one to take - s1 or s2
SELECT *
FROM carecell
LEFT JOIN staff t1 ON cc.id_pks = t1.id_emp
LEFT JOIN staff t2 ON cc.id_wpks = t2.id_emp
INNER JOIN member ON member.id_member = t1.id_member
I have four tables
1. tbl_threads
2. tbl_comments
3. tbl_votes
4. tbl_users
suppose the currently logged in user_id =3
thread_id = 10
Now I have to retrieve the following data
All the fields from tbl_comments where tbl_comments.thread_id =10
All the fields from tbl_users based on the common key tbl_users.user_id = tbl_comments.user_id
All the fields from tbl_votes Where user_id =3 And tbl_votes.comment_id =tbl_comments.comment_id
How can I perform all this function with one single query?
I have tried the following query but it gives me the wrong results
SELECT tbl_comments.*
, tbl_users.*
, tbl_votes.*
FROM tbl_comments
INNER JOIN tbl_users
on tbl_comments.user_id = tbl_users.user_id
WHERE thread_id= 10
INNER JOIN tbl_votes
on tbl_votes.comment_id = tbl_comments.comment_id
WHERE tbl_votes.user_id= 3
Assuming the column thread_id is in the tbl_comments table, change the first where to and:
SELECT tbl_comments.*
, tbl_users.*
, tbl_votes.*
FROM tbl_comments
INNER JOIN tbl_users
on tbl_comments.user_id = tbl_users.user_id
and tbl_comments.thread_id= 10
INNER JOIN tbl_votes
on tbl_votes.comment_id = tbl_comments.comment_id
WHERE tbl_votes.user_id= 3
And although your question mentions a table named tbl_threads, you don't show any reference to it in your example.
SELECT O.OrderNumber, CONVERT(date,O.OrderDate) AS Date,
P.ProductName, I.Quantity, I.UnitPrice
FROM [Order] O
JOIN OrderItem I ON O.Id = I.OrderId
JOIN Product P ON P.Id = I.ProductId
ORDER BY O.OrderNumber
I can't see what is wrong with this query. I get an error saying:
"column article_id in from clause is ambiguous"
I understand that it may have something to do with table name aliases but not sure of how to fix. If the query was smaller I may be able to work something out but it's pretty confusing to me and every time I change something to try and fix it, something else stops - so I thought I'd ask first.
SELECT bt.topic_title, f.article_id, p.photo_id, ba.title, ba.slug,
IFNULL(c.cnt,0) comments, IFNULL(ph.cnt,0) photos, IFNULL(v.cnt,0) videos
FROM blog_article_followers AS f
LEFT OUTER JOIN (
SELECT article_id, COUNT(comment_id) as cnt
FROM blog_comments
GROUP BY article_id) c
ON f.article_id = c.article_id
LEFT OUTER JOIN (" _
SELECT article_id, COUNT(photo_id) as cnt
FROM photos
GROUP BY article_id) ph
ON f.article_id = ph.article_id
LEFT OUTER JOIN (
SELECT article_id, COUNT(video_id) as cnt
FROM videos
GROUP BY article_id) v
ON f.article_id = v.article_id
LEFT JOIN blog_topics bt ON f.topic_id = bt.topic_id
LEFT JOIN blog_articles AS ba USING (article_id)
LEFT JOIN photos AS p USING (article_id)
WHERE f.member_id = 100 AND p.cover = 1
ORDER BY f.follow_date DESC;
Try replacing this:
LEFT JOIN blog_articles AS ba USING (article_id)
LEFT JOIN photos AS p USING (article_id)
With this
LEFT JOIN blog_articles AS ba ON f.article_id = ba.article_id
LEFT JOIN photos AS p ON f.article_id = photos.article_id
you have to rename the column
LEFT JOIN photos AS p USING (p.article_id)
or to whichever table article_id belongs to