I'm looking to find duplicates in a DB - but also show ALL the duplicate records. My current query:
SELECT 'duplicate' as dup,c.Id, c.CreatedDate, c.email, c.Lead_Grade__c, count(c.email)
FROM contact as c
Inner Join (select c.Id, c.email FROM contact as c group by c.email having count(c.email) > 1) as dup
ON c.email = dup.email
WHERE Lead_Grade__c is null;
This works to provide 1 row for each duplicate record. I want 2 (or more) rows for each duplicate record. So, if record X AND record Y both have the same email, then I'd like to show both.
Any thoughts?
Thanks!
This will join contact to itself and only show records where the email is the same that have more than 1 record (Assuming ID is a unique value)
This results set therefor should only be duplicates.
SELECT A.*
FROM contact A
INNER JOIN CONTACT B
on A.email = B.email
and a.id <> b.ID
ORDER BY A.Email
Related
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.
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.
I want to get multiple values in a single row which matches with primary table. Below are the example tables:
members:
- id
- name
- status
address:
- id
- ref_id(member id)
- address1
- state
contacts:
- id
- ref_id(member id)
- phone
- email
mem_cc
- id
- ref_id(member id)
- category_id
- coverage_id
I'm using below query to create view to get all the records in single view so I can query that view to display a list page:
SELECT a.id, a.name, a.status, b.address1, b.state, c.phone, d.category_id, d.coverage_id
FROM members a LEFT JOIN address b
ON a.id = b.ref_id
LEFT JOIN contacts c
ON a.id = c.ref_id
LEFT JOIN mem_cc d
ON a.id = d.ref_id
Now case like Member A is subscribed with 3 coverages or 3 categories then it'll show me Member A's record three times, I want to get Member A record in table single time with covering all categories and coverages in that single row. Question is how to do that?
I believe you need function "group_concat" when selecting the category:
select a.id,a.name,a.status,b.address1,b.state,c.phone,
group_concat(d.category_id, d.coverage_id)
from members a left join address b on a.id = b.ref_id
left join contacts c on a.id = c.ref_id and left join mem_cc d on a.id = d.ref_id
group by a.id
As DMorillo already said you will have to use grouping. In this way you will get one record for the user and in the different columns you can then group the results as necessary.
If you were thinking of extra columns popping up based on your joins then I don't think this is possible. See if the query below works for your case.
SELECT a.id,
a.name,
a.status,
-- This group_concat will produce something like "5th street - Alabama"
-- separated with newlines
-- Check for NULL values since you are using left joins
GROUP_CONCAT(IFNULL(CONCAT(b.address1, ' - ', b.state), ''))
DELIMITER '\n') AS address,
-- Same goes for phone numbers. Default delimiter is comma.
GROUP_CONCAT(IFNULL(c.phone, '') DELIMITER ','),
-- Now you can group your categories.
GROUP_CONCAT(IFNULL(CONCAT(d.category_id,' ', JOINEDCATEGORYNAME), '') AS category,
GROUP_CONCAT(IFNULL(CONCAT(d.coverage_id,' ', JOINEDCOVERAGENAME), '') AS coverage
FROM members a
LEFT JOIN address b ON a.id = b.ref_id
LEFT JOIN contacts c ON a.id = c.ref_id
LEFT JOIN mem_cc d ON a.id = d.ref_id
-- Here probably your inner joins to categories table and coverage table
GROUP BY a.id
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 have two tables: A and B linked by "group_id".
2 variables I'm using: $keyword, $_SESSION['user_id']
A
group_id
keyword
B
id
group_id
user_id
I want to be able to select all the groups that this user is not in based on a keyword search.
Therefore the goal is to SELECT all the rows in A WHERE the user_id!={$_SESSION['user_id'} for the corresponding group_id in B AND like the keyword.
this is what I tried:
SELECT a.*
FROM a
LEFT JOIN b ON a.group_id=b.group_id
WHERE a.keyword LIKE '%".$keyword."%'
AND b.user_id!=$_SESSION{['user_id']}
GROUP BY group_id
However, it does not find any rows (matches) unless I remove AND b.user_id!=$_SESSION{['user_id']} in which case it will also include groups the user is already in - which is not what I want.
Any help would be appreciated! Thanks
Just move the extra condition into the JOIN ON criteria, this way the existence of b is not required to return a result
SELECT a.* FROM a
LEFT JOIN b ON a.group_id=b.group_id AND b.user_id!=$_SESSION{['user_id']}
WHERE a.keyword LIKE '%".$keyword."%'
GROUP BY group_id
Correct answer is simply:
SELECT a.group_id
FROM a
LEFT JOIN b ON a.group_id=b.group_id and b.user_id = 4
where b.user_id is null
and a.keyword like '%keyword%'
Here we are checking user_id = 4 (your user id from the session). Since we have it in the join criteria, it will return null values for any row in table b that does not match the criteria - ie, any group that that user_id is NOT in.
From there, all we need to do is filter for the null values, and we have all the groups that your user is not in.
demo here
Above answers are correct. Another way to achieve this is;
SELECT a.group_id
FROM a
LEFT JOIN b ON (a.group_id, b.user_id) = (b.group_id, 4)
where b.user_id is null
and a.keyword like '%keyword%'
Complete Example
SELECT * FROM a WHERE a.group_id IN
(SELECT group_id FROM b WHERE b.user_id!=$_SESSION{'[user_id']} AND b.group_id = a.group_id)
WHERE a.keyword LIKE '%".$keyword."%';