SQL: how to select customers who have ordered multiple items - mysql

here is some sample data:
ID Item
1 A
1 A
1 B
2 A
2 A
3 A
3 A
3 A
Question: Im trying to write code so that the only records that are selected are those of customer with ID 1 (ie a customer that has both product A and B). So results should look like this:
1 A
1 A
1 B
I've tried a lot of different things, but I am stuck. I tried self-join, but it doesnt produce what I want:
SELECT a.id, a.item
FROM table1 a Join table1 b on a.id=b.id
WHERE upper(a.item) = 'A'
AND upper(b.item) = 'B';
This will give me the right customer (ie customer 1) but it doesnt pull all 3 records. It just gives 1 row.
the closest similar question is
enter link description here

since you want to see which users match a certain condition and the fetch everything about those users - you need a nested query:
SELECT id,item FROM table1 WHERE id IN(
SELECT a.id
FROM table1 a Join table1 b on a.id=b.id
WHERE upper(a.item) = 'A'
AND upper(b.item) = 'B'
)
I took your working query, and used it in a WHERE clause for a more generic query - should do the trick for you

You could use your query as subselect to get the pid and then output all the pids rows. Like this:
SELECT id, item
FROM table1
WHERE id IN (SELECT a.id
FROM table1 a
JOIN table1 b
ON a.id=b.id
WHERE UPPER(a.item) = 'A'
AND UPPER(b.item) = 'B')

is it this what you are looking for? or may be I didn't understand your question.
SELECT a.pid, a.mname
FROM meds a
WHERE a.pid = 1

Related

MySQL: select elements in one table but not in another table, Im getting wrong results

I have the two following tables named tableA and tableB respectively
tableB contain names of all places.I want to select all Facilities and week from where are facility is in tableA but not in tableB.
Table below shows what im intending to achieve
MySql query below is giving wrong results
select tableA.Week ,tableB.Place
from tableA
RIGHT JOIN tableB
on tableA.Place <> tableB.Place
You need a CROSS join of the distinct Weeks of TableA to TableB and NOT EXISTS to get the result that you want:
SELECT w.Week, b.Place
FROM (SELECT DISTINCT Week FROM TableA) w
CROSS JOIN TableB b
WHERE NOT EXISTS (SELECT 1 FROM TableA a WHERE a.Week = w.Week AND a.Place = b.Place)
See the demo.
Results:
Week
Place
1
C
2
B
2
C

SQL Query to compare two tables for names

I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL

SQL Table Counting and Joining

I have Table A, Column 1.
This table has values such as:
1
2
3
3
4
4
4
5
I have Table B, Column 2.
Which lists certain values, like:
1
3
4
I need a query to Count each unique value in Table A, but ONLY if that value is present in Table B.
So with the above, the end result would be:
1 has a quantity of 1,
3 has a quantity of 2,
and 4 has a quantity of 3.
My only problem is that I do not have this ability. Any help out there?
Based on your question, something like the following should solve your problem.
select b.column1,
count(a.column2)
from tableb as b
inner join tablea as a on b.column1 = a.column2
group by b.column1
Since you wanted only records which are in both tables, I am using an inner join. Then I am just grouping by the ID found in tableb, and getting the count of rows in tablea.
Let me know if you have any problems.
For more information regarding inner join, see : http://www.w3schools.com/sql/sql_join_inner.asp, and for group by, see : http://www.w3schools.com/sql/sql_groupby.asp
I would use an INNER JOIN query with GROUP BY aggregate function
SELECT a.column1,
count(a.column1) as total
FROM tablea a
INNER JOIN tableb b
ON a.column1 = b.column2
GROUP BY a.column1
SELECT column1,COUNT(column1)
FROM table1
WHERE column1 IN
(SELECT DISTINCT column2 FROM table2)
GROUP BY column1
Try this
MsSql
Select Distinct Column1,Count(Column1) Over (Partition by Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
Fiddle Demo
MySQl
Select Column1,Count(Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
group by column1
Fiddle Demo

Select count(id) from one table where that id is present in another table MYSQL

EDIT: Detailed description
Detailed Description:
*in table_a there are 100 members but only 50 of them have records in table_b and only 25 have records in table_b where approved = 1 THEREFORE the value I will need returned by the query is 25*
Hey everyone here is the query I am trying to resolve it will need to return a single result count so I can access with mysql_result($query, 0).
SELECT COUNT(id) FROM table_a WHERE (THIS IS WHERE I AM STUCK)
I need to check if the( count of memberID in table_b WHERE memberID matching each id in table_a and approved in table_b = 1) - is greater than 1
The final result needs to be a count of the number of members that have an entry in table_b.
Sample of table columns that need to access
table_a
-----------------
id
table_b
------------------
id
memberID
approved
Let me know if you need any more details.
Problem Solved
Had to think of it backwards
SELECT COUNT( DISTINCT memberID )
FROM table_b
WHERE approved =1
I do not need to even look at table_a seeing as I am counting the memberID based on table_b
Sometimes the solution is so simple and right in front of you.
Thanks for all the help! I hope this helps other in the future.
I'm assuming you want to get the number of records in table_a that match to table_b, as long as the value in table_b is approved. Therefore, I would join the 2 tables. The inner join ensures that you only consider rows that are in both tables, and the where statement takes care of the approved requirement.
select count(a.id)
from table_a a
join table_b b
on a.id = b.memberID
where b.approved=1
SELECT b.ID, a.ID
FROM table_a a, table_b b
WHERE a.ID = b.ID AND b.ID = 1
SELECT COUNT(*) AS cnt
FROM table_a AS a
WHERE EXISTS
( SELECT *
FROM table_b AS b
WHERE b.memberID = a.id
AND b.approved = 1
)
I'm a bit rusty in SQL, but can this be achieved by specifying the two tables in the query like this:
SELECT COUNT(DISTINCT table_a.id)
FROM table_a, table_b
WHERE table_a.id = table_b.MemberID
AND table_b.approved = 1;
I believe that meets your select criteria.

Get duplicate rows and count based on single column

I need to get the distinct rows based on a single column (code in this case) where there are duplicates of that column value. Along with other information from the row and the number of duplicate rows there are. for example:
ID code ownerName
--------------------------
1 001 Mr. Brown
2 001 Mr. Pink
3 002 Mr. White
4 003 Mr. Blonde
I need this query to return
ID code ownerName count
----------------------------------
1 001 Mr. Brown 2
the duplicate row information does not matter which gets returned, but I'm having trouble combining the distinct codes with the count column.
I've tried a query like this:
SELECT DISTINCT A.code, A.ownerName
FROM Customers A WHERE
EXISTS( SELECT * FROM Customers WHERE code = A.code AND id <> A.id)
order by A.code;
but I'm having trouble getting the count; and with this query
SELECT code, COUNT(*) as numberDuplicates
FROM Customers GROUP BY code HAVING COUNT(*) > 1
I'm having trouble getting other information I don't want to group by. Can anyone help me figure out how to structure the correct query?
If I understand what you are looking for, this should work:
This will select all entries with a non-unique code and return the number of records using that code.
SELECT DISTINCT A.ID, A.Code, A.ownerName, B.Count
FROM Customers A
JOIN (
SELECT COUNT(*) as Count, B.Code
FROM Customers B
GROUP BY B.Code
) AS B ON A.Code = B.Code
WHERE B.Count > 1
ORDER by A.Code;
I think you can try something like below
SELECT TOP 1 C.ID, C.Code, C.OwnerName, C1.NumberDuplicates
FROM
Customers C
INNER JOIN
(
SELECT Code, COUNT(*) as NumberDuplicates FROM Customers GROUP BY code HAVING COUNT(*) > 1
) C1
ON C.Code = C1.Code
Hope this Helps!!
SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers FROM Orders;