I have four table like the picture bellow
I want to count how many student that have status 'v' where in table submission have submission type '1' and group by student_id so in the last i can get table like this
I have try sql query like this
select p.id, (SELECT count(*) FROM (select b.id from student as a , submission as b WHERE a.id = b.student_id and b.id_submission_type =1 and a.status_n='v' and a.id_academic_programe = p.id GROUP BY b.student_id) ) from academic_programe as p
But give me error
1054 - Unknown column 'p.id' in 'where clause'
Any suggestion? sory for my english
Correlations cannot be in nested subqueries. Fortunately, this is easy to fix:
select p.id,
(select count(*)
from student st join
submission su
on st.id = su.student_id and
su.id_submission_type = 1 and
st.status_n = 'v' and
where st.id_academic_programe = p.id
)
from academic_programe p;
Try this out:
select c.academic_program_name,count(a.distinct student_name) as count
from
(select * from student where status = 'v') a
inner join
(select * from submission id_submission_type=1) b
on a.id =b.student_id
inner join
academic_program_name c
on a.id_academic_programe = c.id
group by c.academic_program_name;
Let me know in case of any queries.
Please try the following...
SELECT student.id,
student_name,
academic_program_name AS Programe,
COUNT( status_n ) AS status_n_count
FROM student
JOIN Submission ON student.id = Submission.student_id
RIGHT JOIN academic_program ON student.id_academic_programe = academic_program.id
WHERE id_submission_type = 1
AND status_n = 'v'
GROUP BY student.id,
student_name,
academic_program_name;
This statement begins by joining the student and Submission so as to get a table containing the student's id, student_name, status_n and id_submission_type fields. This is then RIGHT JOINed to form a table where each academic program is listed along with each student's details, and that programs with no students are still listed.
The resulting dataset is refined as per your criteria with the WHERE clause, GROUPed and SELECTed
If you have any questions or comments then please feel free to post a Comment accordingly.
Related
I would like to check each store's average score.
my ‘shop’ table is
my ‘order’ table is
My SQL is :
SELECT a.id,a.name,AVG(b.point) as point
from shop a
LEFT JOIN order b on a.id=b.shop_id
WHERE b.point<>0
GROUP BY(b.shop_id)
but this SQL only can check out the shop had point in ‘order’ table.
How can i get whole shop list in SQL?
Please give this a try:
SELECT
a.id,
a.NAME,
AVG(b.point) AS point
FROM
shop a
LEFT JOIN `ORDER` b ON a.id = b.shop_id AND b.point <> 0
GROUP BY a.id
Since b.shop_id can be NULL so that you need to group by a.id in order to get result for all shop ids.
It works but what I would like is to list username, first name, last name not all but I've tried JOIN and isn't seeming to work. Any ideas? Thanks!
MY DB:
http://gyazo.com/eb13cd68440d20719ce0783018cb9828
SELECT
M.Username,
M.first_name,
M.Last_name,
COUNT(1) AS num_comments
FROM members AS M
INNER JOIN comments AS C
ON C.memberID = M.memberID
GROUP BY
C.memberID
ORDER BY COUNT(1) DESC
LIMIT 1
This matches the Member to all their comments, groups by the member to get the count of comments for the users, orders by the count starting highest first, then returns the first result.
Instead of selecting * (ALL) just use SELECT table.username, table.firstname, table.lastname [...].
You can leave out the table. if all information is stored in your comments table. If not, adjust accordingly. In that case you'll also need to Join the comments table with the table where the rest of the information is stored.
Edit:
SELECT m.username, m.first_name, m.last_name FROM members m, comments c WHERE m.MemberID = c.MemberID AND c.author = (select max(author) from comments)
Table 1 :- tbl_contacts
Fields
user_id
contact_id
first_name
last_name
Table 2 :- tbl_phone_details
Fields
contact_id
phone_number
phone_type
Table 3 :- tbl_email_details
Fields
contact_id
email_address
email_type
QUERY -
SELECT
tbl_contacts.*, tbl_email_details.*, tbl_phone_details.*
FROM
tbl_contacts, tbl_email_details,
tbl_phone_details
WHERE
tbl_contacts.user_id = '1'
I want to get first_name, last_name, Phone and Email details of particular user_id. I have used above query but its giving me repeated results and I am having less knowledge on DB queries like JOIN and INNER QUERY.
If anyone has any idea, please kindly help.
OUTPUT NEEDED:-
contact_id, first_name, last_name, phone_number, phone_type, email_address, email_type
(Here email and phone number can have 1 or more values for particular users).
Try like this
If you want to retrieve data for particular ID
SELECT T.contact_id,
T.first_name,
T.last_name,
P.phone_number,
P.phone_type,
E.email_address,
E.email_type
FROM tbl_contacts T LEFT JOIN tbl_phone_details P ON
T.contact_id = P.contact_id
LEFT JOIN tbl_email_details E ON
T.contact_id = E.contact_id
WHERE T.contact_id = #contact_id
If you want to retrieve all data
SELECT T.contact_id,
T.first_name,
T.last_name,
P.phone_number,
P.phone_type,
E.email_address,
E.email_type
FROM tbl_contacts T LEFT JOIN tbl_phone_details P ON
T.contact_id = P.contact_id
LEFT JOIN tbl_email_details E ON
T.contact_id = E.contact_id
SELECT tbl_contacts.*, tbl_email_details.*, tbl_phone_details.* FROM
tbl_contacts, tbl_email_details, tbl_phone_details WHERE
tbl_contacts.user_id = '1'
You forgot to mention the condition by which you are going to join all the tables!
SELECT c.first_name, c.last_name, p.phone_number, e.email_address
FROM tbl_contacts c, tbl_email_details e, tbl_phone_details p
WHERE tbl_contacts.user_id = '1'
AND c.contact_id = e.contact_id
AND e.contact_id = p.contact_id;
SELECT c.contact_id, c.first_name, c.last_name,
phone.phone_number, phone.phone_type,
email.email_address, email.email_type
FROM tbl_contacts c
LEFT JOIN tbl_email_details email ON c.contact_id = email.contact_id
LEFT JOIN tbl_phone_details phone ON c.contact_id = phone.contact_id
WHERE tbl_contacts.user_id = '1'
Sql queries are easy to learn and write and they are very useful in getting the important data from database. Joins are used to basically fetch the data from two or more tables on the bases of common column in both tables.
Inner Join will select those values that are common in both of the tables.
Left Join will select all the data from the left table and Right Join will select all the data from right table on the basis of id.
These are the basics of SQL and you must know how to fetch accurate data using them.
this is how I would make that request with JOIN ... but there might be some better or faster way to do it.
SELECT first_name, last_name, phone_number, email_address
FROM tbl_contacts
JOIN tbl_phone_details
ON tbl_contacts.contact_id=tbl_phone_details.contact_id
JOIN tbl_email_details
ON tbl_email_details.contact_id=tbl_contacts.contact_id
WHERE tbl_contacts.user_id = '1';
And just so you don't get lost in all the different answers here (which are probably all correct):
you don't need to give an aliase name to your tables (it's just for readability)
you don't need to mention the table names in your column list if the column name is unique (e.g first_name is only in tbl_contacts). Just if you want the contact_id then you should decide which one (e.g. tbl_phone_details.contact_id)
the multi-select as Jayaram proposed, is exactly the same as the JOIN. MySQL handles both queries the same way (I just didn't see his answer when I responded, sorry)
I am trying to query two tables that have foreign keys in each other to display conditional results, my tables are as follow:
auction_bids table:
id applicant_id bid auction_id owner.id
auctions table:
id user_id title description
The result I need to display to the logged in user is:
title applicant_id [how many bids the project has] auction_id
My current not working query is:
SELECT (SELECT COUNT(auction_bids.owner_id)
FROM auction_bids
WHERE auction_bids.owner_id = ".$_SESSION['userid']."
) AS count, auction_bids.*
FROM auction_bids
WHERE auction_bids.owner_id = ".$_SESSION['userid']."
i cannot get how many bids each auction has in the database along with the title
Thank you
This one will list all applicants and their total bids on all auctions the selected user have:
SELECT a.title, b.applicant_id, COUNT(b.applicant_id) AS bids, a.id
FROM auction_bids b
JOIN auctions a ON a.id = b.auction_id
WHERE b.owner_id = ".$_SESSION['userid']."
GROUP BY b.applicant_id
See a live DEMO here.
Keep in mind you on your question you have owner.id like that while here I have it with an underscore owner_id change it as you need.
According to your comments on the other reply you may want a different result per auction from what you propose on your question.
This one will list all auctions from a user with the total bids, auction title and id:
SELECT a.title, COUNT(b.applicant_id) AS total_bids, a.id
FROM auction_bids b
JOIN auctions a ON a.id = b.auction_id
WHERE b.owner_id = ".$_SESSION['userid']."
GROUP BY b.auction_id
See a live DEMO here.
I think you want a GROUP BY:
SELECT a.title, ab.applicant_id, COUNT(*), a.id
FROM auction a JOIN
auction_bids ab
ON a.id = ab.auction_id
WHERE a.user_id = ".$_SESSION['userid']."
GROUP BY a.id
I have the following query which gives me the right results. But it's super slow.
What makes it slow is the
AND a.id IN (SELECT id FROM st_address GROUP BY element_id)
part. The query should show from which countries we get how many orders.
A person can have multiple addresses, but in this case, we only only want one.
Cause otherwise it will count the order multiple times. Maybe there is a better way to achieve this? A distinct join on the person or something?
SELECT cou.title_en, COUNT(co.id), SUM(co.price) AS amount
FROM customer_order co
JOIN st_person p ON (co.person_id = p.id)
JOIN st_address a ON (co.person_id = a.element_id AND a.element_type_id = 1)
JOIN st_country cou ON (a.country_id = cou.id)
WHERE order_status_id != 7 AND a.id IN (SELECT id FROM st_address GROUP BY element_id)
GROUP BY cou.id
Have you tried to replace the IN with an EXISTS?
AND EXISTS (SELECT 1 FROM st_address b WHERE a.id = b.id)
The EXISTS part should stop the subquery as soon as the first row matching the condition is found. I have read conflicting comments on if this is actually happening though so you might throw a limit 1 in there to see if you get any gain.
I found a faster solution. The trick is a join with a sub query:
JOIN (SELECT element_id, country_id, id FROM st_address WHERE element_type_id = 1 GROUP BY
This is the complete query:
SELECT cou.title_en, COUNT(o.id), SUM(o.price) AS amount
FROM customer_order o
JOIN (SELECT element_id, country_id, id FROM st_address WHERE element_type_id = 1 GROUP BY element_id) AS a ON (o.person_id = a.element_id)
JOIN st_country cou ON (a.country_id = cou.id)
WHERE o.order_status_id != 7
GROUP BY cou.id