Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is my current code
SELECT * FROM pn_queue WHERE email = '2'
Now i want to extend is with an "AND" wich is in another table. Like this:
SELECT * FROM pn_queue WHERE email = '2' AND FROM pn_cats WHERE cat = '2'
How can i make it? I will be thankfull for any ideas or solutions.
You need to use a Join
select * from pn_queue a
join pn_cats b on a.pn_cats_id = b.id -- Change this condition to whatever should match on both table.
where b.cat = '2'
and a.email = '2'
Not sure if the join condition is actually right as you don't give enought information, but just modify the condition a.pn_cast_id = b.id if it ain't.
Edit : Just realized in your example cat and email are both equals to 2 so if it should be the match value then the correct query would simply be :
select * from pn_queue a
join pn_cats b on b.cat = a.email
For more information on SQL Join, you should check this thread What is the difference between "INNER JOIN" and "OUTER JOIN"?. It explain the difference between all possible joins with diagrams/examples etc.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm building library which generates SQL queries and found case which i don't understand why it is happening so. I agree that in general you should filter out by "where" and not "on" yet ON allows to filter out for LEFT join. Though for RIGHT join i get results that do not match ON request. I wonder why RIGHT join works this way.
Users (table name _user):
Invoices (table name _invoice):
Query:
SELECT
_user.id AS userId,
_user.name AS userName,
_user.state AS userState,
_invoice.id AS invoiceId,
_invoice.userId AS invoiceUserId,
_invoice. `state` AS invoiceState
FROM
_user
RIGHT JOIN _invoice on _invoice.state = 'pending'
Response:
Question:
Would be best to get step-by-step execution for this query-result to understand how exactly it happen.
A right join keeps all rows from the second table regardless of whether the on clause evaluates to "true", "false", or NULL.
Presumably, though, you want a valid join condition. My guess is you want a list of all users and any pending invoices. If so, the correct logic would be:
SELECT . . .
FROM _user u LEFT JOIN
_invoice i
ON i.userid = u.id AND i.state = 'pending';
At the very least, this produces a result that seems usable.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables:
request(RequestID,Name,Mobie Number,Vehicle Number,Location)
reqresponse(RequestID,ResponseID,MechanicID,MechanicName)
mechdetails(MechanicID,MechanicName,MechanicMobile,MechanicAvalability)
Now, what I want to do is select all data from the request table and display it along with details of the mechdetails table, where MechanicAvailability is set to 'busy'.
RequestID is the foreign key in reqresponse table.
Can anyone please tell me the MySQL code to do so.
I am working in PHP.
This is where I am right now:
SELECT * FROM mechdetails WHERE MechAvailability='Busy' AND
MechanicID=(SELECT MechanicID FROM reqresponse WHERE ResponseID='$rid')
I am really new to MySQL and PHP so please help me!
The query
SELECT * FROM request r
JOIN reqresponse q ON q.RequestID = r.RequestID
JOIN mechdetails m ON m.MechanicID = q.MechanicID
WHERE MechanicAvailability = 'busy'
should do the job.
To access the resulting data, use mysqli_query and mysqli_result.
You can join the tables:
select r.*, m.*
from request r
inner join reqresponse rr on rr.requestid = r.requestid
inner join mechdetails md on md.mechanicid = rr.mechanicid
where md.mechanicavailability = 'busy'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My query:
SELECT DISTINCT img.prd_id,prd.*,img.* FROM products_prd prd OUTER JOIN
prd_images_img img ON prd.prd_id=img.prd_id ORDER BY prd.prd_datetime
the primary key in products_prd is returned NULL. They have values. while img.prd_id may be NULL or not.
The problem is that you have name collisions -- two things called prd_id for example. You need to use aliases to rename the columns:
SELECT prd.*, img.col1 as img_col1, img.col2 as img_col2
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
ON prd.prd_id = img.prd_id
ORDER BY prd.prd_datetime;
You don't need to select prd_id twice.
Or, you can use USING instead of ON:
SELECT *
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
USING (prd_id)
ORDER BY prd.prd_datetime;
This returns the columns in the USING clause only once (although you could have problems with other columns if they have the same names in the two tables).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two tables
Table A : Content Question , its options and correct answer
Table B : This table is for time allow to answer the question in given time.
this table has Question_Id field which either have question id or zero. zero means if for Table A Question Id is not found in Table B then default Time will be Table B's Question_Id=0 > 5 Min
Now I want the data like Result table from query. By using Select Query with Join I am getting question details, which are matched with question Id (1,2,4) means for Question 3,5,6 row not getting that showing in result table.
Please suggest what sql query should write so that I can get result like Result Table's content.
I change your tables to small and simple tables and you can see the result in:
SQL Fiddle
or try this query:
SELECT t1.questionid,
t1.question,
t1.options,
t1.answer,
COALESCE(t2.timingstatement, '5 Min') TimingStatement
FROM tablea t1
LEFT OUTER JOIN tableb t2
ON t1.questionid = t2.questionid;
Try this
SELECT Q.QuestionID,Q.Question,Q.Options,QAnswer,
CASE WHEN Q.QuestionID NOT IN (SELECT QuestionID FROM Table2) THEN '5 Min'
ELSE T.TimingStatement
END [TimingStatement]
FROM Table1 Q
JOIN Table2 T ON Q.QuestionID = T.QuestionID