Can anyone help me please? Inner join query working fine. but query displaying duplicate data. I don't to display duplicate data.
here is my query.
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
Here is output
"COLOR";"456";"456";"Nude"
"COLOR";"456";"456";"Ivory"
"COLOR";"456";"456";"Black"
"COLOR";"456";"456";"Coral"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Coral"
"COLOR";"459";"459";"Nude"
"COLOR";"459";"459";"Ivory"
"SIZE";"460";"460";"Large"
"SIZE";"460";"460";"Medium"
"SIZE";"460";"460";"Small"
"SIZE";"470";"470";"Large"
"SIZE";"470";"470";"Small"
"SIZE";"470";"470";"Medium"
"COLOR";"476";"476";"White"
"COLOR";"476";"476";"Black"
"SIZE";"477";"477";"Small"
But i don't to display duplicate data. for example which is displaying here.
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"460";"60";"Black"
is there any way?? thanks
Maybe you just want to group it by names? You seem to be calling a duplicated data what seems to have different ids...
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
GROUP BY t1.class,t2.option_name
Related
Hi I am new to sql I just want to ask for advice my query is working fine it gives me my desired output but I wanted to make it in one query only I want to combine my delete query to the select query. I would really appreciate anyone can teach me how to do it
Delete from tblexample where RStatus = 'Done';
SELECT t3.RStatus,t1.Name,t2.gender,t1.Rnum,t1.NB,t1.fN
FROM table1 t1
INNER JOIN table2 t2
ON (t1.fN = t2.fln)
LEFT JOIN (
select * from tblexample t3
) AS t3 ON t1.NB = t3.NB where t3.RStatus is null
Like if possible I want to delete firs before I left join the tblexample
I am trying to use join to connect multiple tables in MS Access to get count values. But I don;t know it gives wrong count values. If I try to join them individually, then it gives me correct count values.
I have 3 Tables. Table 2 and Table 3 are independent and are connected to Table 1. Test 2 and test 3 are basically text values and I want to count the rows .
Table1(ID1(Primary Key),Name)
Table2(ID2(Primary Key), ID1(Foreign Key), Test2)
Table3 (ID3(Orimary Key), ID1(Foreign Key), Test3)
The Query that I get from MS Access is given below:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3
FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2)
INNER JOIN Table3 ON Table1. ID1 = Table3.ID3
GROUP BY Table1.ID1;
But this gives me wrong Count Values.
Can someone please help me.
Thanks.
When I use it individually, it gives me correct count value:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1
GROUP BY Table1.ID1;
SELECT Table1. ID1, Count(Table3.Test3) AS CountOfTest3
FROM Table1 INNER JOIN Table3 ON Table1.ID1 = Table3.ID1
GROUP BY Table1.ID1;
But when I try to join Table1, Table2 and Table 3 in MS Acces, it gives me incorrect count values.
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3 FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1) INNER JOIN Table3 ON Table1. ID1 = Table3.ID1 GROUP BY Table1.ID1
As per my understanding it is taking the count value of 1st query in the parenthesis and multiplying it with the count values of the other inner join.
I have tried many things but don't know what to do. Access adds parenthesis for some reason.
If I can assume test2 and test 3 are unique to each record (perhaps it would be better to count the PK?)
SELECT Table1.ID1
, Count(distinct Table2.Test2) AS CountOfTest2
, Count(distinct Table3.Test3) AS CountOfTest3
FROM Table1
INNER JOIN Table2
ON Table1.ID1 = Table2.ID2
INNER JOIN Table3
ON Table1.ID1 = Table3.ID3
GROUP BY Table1.ID1;
Or you may have to get the counts before the joins though the use of inline views. You could use window functions if MSSQL SERVER but Access needs the inline views.
SELECT A.ID1
, B.CountOfTest2
, C.CountOfTest3
FROM Table1 A
INNER JOIN (SELECT Table2.ID2, count(table2.test) as CountOfTest2
FROM Table2
GROUP BY Table2.id) B
ON Table1.ID1 = B.ID2
INNER JOIN (SELECT Table3.id, count(table3.test3) as CountOfTest3
FROM Table3
GROUP BY Table3.id) C
ON B.ID1 = C.ID3
GROUP BY A.ID1;
Yup i had the same problem when i was trying to do this. just use the double sql function to counteract the html and you should be good. once the query has been doubled it will react like a C++ quota statement. If this fails you can always just quantify the source fields to adhear to the table restrictions. its actually a piece of cake i hope this helped.
I need to count how many users are in each group in a database. Unfortunately the database design is not great and the users uids are stored against the group in the group table in a LONGTEXT field column name owncloudusers.
Example of owncloudusers data :
{i:0;s:36:"25C967BD-AF78-4671-88DC-FAD935FF1B26";i:1;s:36:"40D6866B-EA06-4F39-B509-8CE551CC1924";i:2;s:36:"7724C600-DE23-45C8-8BFD-326B0138E029";i:3;s:36:"D6FF37EC-11F4-471F-94C9-F3A28416CF1F";i:4;s:36:"F70C6D03-B7BA-44E4-B703-9AF3EED9BC03";}
I thought I could use a query with a LIKE on the join to compare the user's uid and look inside owncloudusers and see if there is a match.
The closest I have got is:
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers LIKE('%:"'||T2.owncloud_name||'";%')
GROUP BY owncloudname;
T1 table holds the groupings and who is tagged to that group
T2 table holds the users data. column owncloud_name is the users uid column
I have tried a few approaches I found on stackoverflow CONCAT on the LIKE join and LIKE('%:"'+T2.owncloud_name+'";%')
But no joy. The current statement I have returns 0 users against all the groups but I know this is not right.
I know it much but an issue on the join not sure where to go with it next.
Any assistance would be much appreciated.
I think you need a simple
SELECT T1.owncloudname, count(*) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers LIKE '%T2.owncloud_name%'
GROUP BY owncloudname;
If you need concat try
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers
LIKE concat( '%',T2.owncloud_name,'%' )
GROUP BY owncloudname;
You were close, but mysql doesn't understand || as a text concatenation operator; use CONCAT() with the text parts passed as a list of values to build the LIKE operand:
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2
ON T1.owncloudusers LIKE CONCAT('%;', T2.owncloud_name, ';%')
GROUP BY owncloudname;
if there aint any performance issue,
could you try it with sub-query,
SELECT
T1.owncloudname,
(SELECT COUNT(*)
FROM oc_ldap_user_mapping AS T2
WHERE LOCATE(T2.owncloud_name,T1.owncloudusers)=1) AS Users
FROM
oc_ldap_group_members T1
GROUP BY
owncloudname;
So here is the thing, I have two tables:
table1 has columns intUsersID, varUsersName
table2 has columns intCouriers, intResponsible
intCouriers (have some numbers of intUsersID that are Couriers), and intResponsible (have some numbers of intUsersID that are Responsible)
In my query I must see User Names of Couriers and of the Responsible persons
something like that:
SELECT
table1.varUsersName 'Couriers',
table1.varUsersName 'Responsible'
FROM
table1
LEFT JOIN
table2 ON table2.intCouriers = table1.intUsersID
And then I need some how to subquery or join this "table1.varUsersName 'Responsible'", to get also 'Reponsible' persons. Please help me.
Should be this
SELECT table1.varUsersName 'Couriers', table2.varUsersName 'Responsible'
FROM table1
INNER JOIN table3 on table1.intUsersID = table3.intCouriers
INNER JOIN table1 as Table2 on table2.intUsersID = table3. intResponsible
SELECT Couriers.varUsersName as "Couriers",
Responsible.varUsersName as "Responsible"
FROM `table2` t2
LEFT JOIN table1 Couriers on Couriers.intUsersID = t2.intCouriers
LEFT JOIN table1 Responsible on Responsible.intUsersID = t2.intResponsible
I want to display the ID from Table1 (TID) and the results of an inner join.
The following statement is not working.
Situation: Two Tables:
Table 1 PK:TID, FK: Table2_PID
Table2 PK: PID, Name
Among other data I want to display the Name of every PID in Table1 which is stored in Table2.
SELECT T.TID
,(Select P.Name
from mydb.Table2 P
inner join mydb.Table1 T
on P.PID=T.Table2_PID)
FROM mydb.Ticket T;
Result: Error Code 1242. Subquery returns more than 1 row
I do know that the result returns more than 1 row, but I want to show the Name of every PID in Table1 which is stored in Table2. So any ideas on how I can do that?
PS: I'm using mySQL and working with MySQL Workbench v6.3
You don't need use the inner joins for getting all the names of the ID. You can try the default join to achieve the result.
select t2.pid, t2.name from mydb.Table2 t2, mydb.Table1 t1 where t1.pid = t2.pid;
Hope this helps.
You must use join like this
select t1.TID,t2.Name from Table1 t1 left join Table2 t2 on t1.Table2_PID=t2.PID
Thanks for the response, but the question/problem still remains.
It wasn't about the join.
It is about the subquery and selecting multiple rows within it.
Thank you guys,
I was thinking of a solution way to complicated. I resolved it using a simple where statement.
SELECT T.TID, P.Name
FROM mydb.table1 T, mydb.table2 P
WHERE P.PID=T.table2_PID;