SQL select and compare - mysql

I am trying to retrieve everything from client and bond table where the client.id is not equal to bond.client and bond client.bond is not equal to bond.id. My query did not work and returns the whole list instead. How can i solve this problem? My query wants to output 4 , 5 from client table and 5, 5 from bond table as the result
Client table
Id Bond
1 2
2 3
4 5
Bond table
Id Client
2 1
3 2
5 5
.
SELECT * FROM `client_table`
INNER JOIN `bond_table`
where client_table.id != bond_table.client and client_table.bond != bond_table.id

Please try this:
SELECT c.ClientID, c.BondID from client_table c
left join bond_table b
on c.clientID = b.clientID
where b.clientID is null
UNION
SELECT b.ClientID, b.BondID from bond_table b
left join client_table c
on c.clientID = b.clientID
where c.clientID is null

could be using not in clause
SELECT * FROM `client_table`
INNER JOIN `bond_table` on client_table.id
not in ( select bond_table.client from bond)
AND client_table.bond not in ( select bond_table.id from bond)
but based on the resukt you show in the comment you should use OR (and not AND)
SELECT * FROM `client_table`
INNER JOIN `bond_table` on client_table.id
not in ( select bond_table.client from bond)
OR client_table.bond not in ( select bond_table.id from bond)

Related

Issue getting while fetching data using joins

I have four table and structure as below :
1)Budget
id Budget_name
1 test1
2 test2
3 test3
2)Yearly Budget
id amount_yearly budgetid
1 1000 1
2 2000 2
3 5000 3
ri_spent
id Spent_amount budgetid
1 100 2
2 100 2
3 200 3
4)FI_spent
id Spent_amount budgetid
1 100 2
2 100 3
3 200 3
i want to fetch data accourding to or based on first budget table id
below is the query i was trying:
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d
inner join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
inner join RI_DETAILS as l on l.COST_CENTER = d.id
inner join F_RI_DETAILS as f on f.COST_CENTER = d.id
ORDER BY d.id DESC
I want output of the following way:
Id Name ri_id FI_ID RI_Spent_Amount FI_Spent_Amount Annual_Buget
1 test1 1000
2 test2 1 1 100 200 1000
2 test2 2 100 2000
3 test3 3 2 300 100 2000
3 test3 3 200 2000
Any way if possible then please help me.
I want to minus annual budget with spent_amount later.
If possible then help me .
Please Try This Query ,
select b.id ,
b.Budget_name,
y.id,
y.amount_yearly,
r.id,
r.ri_spent,
f.id,
f.Spent_amount
from Budget b
INNER JOIN Yearly_Budget y on y.budgetid =b.id
INNER JOIN ri_spent r on r.budgetid =b.id
INNER JOIN FI_spent f on f.budgetid =b.id
ORDER BY b.id DESC
Could you please use left join instead of inner join. Requirement and sql arent matching so i may be little off. But you can get the idea.
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d -- I assume this is the budget table
left outer join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
left outer join RI_DETAILS as l on l.COST_CENTER = d.id
left outer join
(select sum(FINANCEADD_ID) FINANCEADD_ID,sum(AMOUNT) AMOUNT,id from
F_RI_DETAILS group by id) as f
on f.COST_CENTER = l.id
ORDER BY d.id DESC

MySQL query select from multiple table depending on a certain condition

I have two tables as follow:
table internetclient
(id,full_name,location,phone_number)
table internetclientdetails
(incdid,icid,date_sub, date_exp,isPaid,profile_sub)
the data in two table is as follow:
client
--------------------------------------------------------
id full_name location phone_number
-------------------------------------------------------
4 Joe Amine beirut 03776132
5 Mariam zoue beirut 03556133
client_subscription
--------------------------------------------------------------------------
incdid icid date_sub date_exp isPaid sub_price
----------------------------------------------------------------------------
6 4 2018-01-01 2018-01-30 0 2000
7 5 2017-01-01 2017-01-30 0 1000
8 4 2018-03-01 2018-03-30 1 50000
9 5 2018-05-01 2019-05-30 1 90000
note : incdid stands for internetClientDetailsId
and icid stands for internetClientId
Problem
I want to make a query that return client name along with all details depending on the latest client subscription date, the result should be as follow:
------------------------------------------------------------
full_name client_id date_sub sub_price
------------------------------------------------------------
Joe Amine 4 2018-03-01 50000
Mary 5 2018-05-01 90000
What i am tring
SELECT * FROM client c LEFT JOIN client_subscription c_s on c.id=c_s.client_id
UNION
SELECT * FROM client c RIGHT JOIN client_subscription c_S on c.id=c_s.client_id
WHERE
c.sub_date=(SELECT MAX(sub_date) from client_subscription c_s INNER JOIN client c on c.id=c_s.client_id GROUP BY c_s.client_id
i have been working on it all the night. Any help is appreciated a lot.
To get client_subscription for each client you could use a self join
select c.name, a.client_id, a.date_sub, a.sub_price
from client_subscription a
join (
select client_id, max(date_sub) date_sub
from client_subscription
group by client_id
) b on a.client_id = b.client_id and a.date_sub = b.date_sub
join client c on a.client_id = c.id
order by a.date_sub
Demo
Or using left join
select c.name, a.client_id, a.date_sub, a.sub_price
from client_subscription a
left join client_subscription b on a.client_id = b.client_id and a.date_sub < b.date_sub
join client c on a.client_id = c.id
where b.client_id is null
order by a.date_sub
Demo
Using your updated data set updated queries are
select c.full_name, a.icid, a.date_sub, a.sub_price
from internetclientdetails a
join (
select icid, max(date_sub) date_sub
from internetclientdetails
group by icid
) b on a.icid = b.icid and a.date_sub = b.date_sub
join internetclient c on a.icid = c.id
order by a.date_sub;
select c.full_name, a.icid, a.date_sub, a.sub_price
from internetclientdetails a
left join internetclientdetails b on a.icid = b.icid and a.date_sub < b.date_sub
join internetclient c on a.icid = c.id
where b.icid is null
order by a.date_sub
Updated Demo
Hi try below sample might help you.
DECLARE #tblClient AS TABLE (ID INT , Name varchar(100))
DECLARE #tblClientSub As TABLE (id INT,client_id INT,date_sub DATE,sub_price INT)
INSERT INTO #tblClient (id,Name)
VALUES
(1,'Linda'),
(2,'Mary'),
(3,'Joe')
INSERT INTO #tblClientSub(Id,client_id , date_sub , sub_price)
VALUES
(1,1,'2018/01/01',50),
(2,2,'2018/02/01',50),
(3,2,'2018/03/01',30),
(4,2,'2018/04/01',30),
(5,3,'2018/01/01',50),
(6,3,'2018/07/01',50),
(7,1,'2018/02/01',40)
SELECT c.Id,c.Name,cs.date_sub,cs.sub_price
FROM #tblClient c
CROSS APPLY (SELECT TOP (1)date_sub,sub_price
FROM #tblClientSub
WHERE client_id = c.Id
ORDER BY date_sub DESC) cs
select c.name as 'client_name',cs.client_id,max(cs.sub_date) as 'date_sub',cs.sub_price from client c ,
client_subscription cs where cs.client_id=c.id group by cs.client_id,cs.sub_price;
Try this
SELECT c.Name, c.id , MAX(date_sub), sub_price FROM client c LEFT JOIN client_subscription c_s on c.id=c_s.client_id
GROUP BY c.id
ORDER BY c.id ASC

How to GROUP BY an ID the result of a SUM with a JOIN

I have two Tables, one with my contacts informations (contact) and one with the users interactions (notitia).
Here's an exemple :
SELECT n.contact_id, c.email, c.numero, c.code_postal, n.nbr_click, n.nbr_ouverture,
(
n.nbr_click * 2
)
+
(
n.nbr_ouverture * 3
)
as total FROM notitia n LEFT JOIN contact c ON c.id = n.contact_id LIMIT 5
The result is :
Now I'm Trying to do the SUM of the nbr_click, nbr_ouverture and total if the contact_id is the same, In my exemple we have 2 times the contact_id 3 so the result should be :
I tried to GROUP BY n.contact_id :
SELECT n.contact_id, c.email, c.numero, c.code_postal, n.nbr_click, n.nbr_ouverture,
(
n.nbr_click * 2
)
+
(
n.nbr_ouverture * 3
)
as total FROM notitia n LEFT JOIN contact c ON c.id = n.contact_id GROUP BY n.contact_id LIMIT 5
But here only the first Row with the contact_id = 3 is taken
I also tried to do the SUM :
SELECT n.contact_id, c.email, c.numero, c.code_postal, SUM(n.nbr_click), SUM(n.nbr_ouverture),
(
SUM(n.nbr_click) * 2
)
+
(
SUM(n.nbr_ouverture) * 3
)
as total FROM notitia n LEFT JOIN contact c ON c.id = n.contact_id LIMIT 5
But here everything is added in on row with enormous result
You should have kind of the mix of the two queries you have written. You should GROUP BY contact_id and then apply SUM to the columns for which you need the SUM. Try this:
SELECT n.contact_id,
c.email, c.numero, c.code_postal, SUM(n.nbr_click), SUM(n.nbr_ouverture),
(
SUM(n.nbr_click) * 2
)
+
(
SUM(n.nbr_ouverture) * 3
)
as total FROM notitia n
LEFT JOIN contact c ON c.id = n.contact_id
GROUP BY n.contact_id LIMIT 5

Sql server join 3 tables and get the latest from 3rd table

Hi all I am having 3 tables as follows
Technology_table
Technology_ID Technology_Name
10 Asp.Net
20 C#
Question_table
QUESTION_ID Technology_ID QUESTION_DESCRIPTION
1 10 First ques in Asp.net
2 20 First ques in C#
Reply_table
QUESTION_ID Technology_ID Reply_Date Reply_Message
1 10 2016-01-23 I am first to post
1 10 2016-01-24 I am second to post
I have written the following query but not getting the result as expected
select
FI.QUESTION_ID,FI.QUESTION_TITLE,FI.USER_NAME,FI.DATE_POSTED,
FI.[DATE_REPLIED],FI.RepliedName,FI.VIEW_COUNT,FI.REPLY_COUNT,
FI.REPLY_MESSAGE,TT.TECHNOLOGY_ID,TT.TECHNOLOGY_NAME
from FORUM_TECHNOLOGY TT,
( select distinct
TQ.TECHNOLOGY_ID,TQ.QUESTION_ID,TQ.QUESTION_TITLE,TQ.USER_NAME,
TQ.DATE_POSTED,
TR.[DATE_REPLIED],
TR.USER_NAME as RepliedName,
TQ.VIEW_COUNT,TQ.REPLY_COUNT,TR.REPLY_MESSAGE
from FORUM_QUESTIONS TQ
LEFT OUTER JOIN FORUM_REPLIES TR
ON TR.TECHNOLOGY_ID=TQ.TECHNOLOGY_ID
and TR.QUESTION_ID = TQ.QUESTION_ID
and TR.[DATE_REPLIED] in (
select MAX(TR.[DATE_REPLIED])
from FORUM_REPLIES TR
group by TR.QUESTION_ID
)
) FI
where FI.TECHNOLOGY_ID =TT.TECHNOLOGY_ID
and TT.TECHNOLOGY_ID = #TechID
I also tried in this way
select t1.QUESTION_ID,oa.USER_NAME,oa.REPLY_MESSAGE
from FORUM_QUESTIONS t1
cross apply(select top 1 * from FORUM_REPLIES
where QUESTION_ID = t1.QUESTION_ID
order by DATE_REPLIED desc)oa
join FORUM_TECHNOLOGY t2 on oa.TECHNOLOGY_ID = t2.TECHNOLOGY_ID
AND oa.QUESTION_ID = t1.QUESTION_ID
I would like to display only one instead of duplicates
Can some one help me
This assumes that your DATE_REPLIED has a valid time component and isn't just omitted or defaulted to midnight.
SELECT FQ.QUESTION_ID
, FQ.QUESTION_TITLE
, FQ.USER_NAME
, FQ.DATE_POSTED
, FR.DATE_REPLIED
, FR.RepliedName
, FQ.VIEW_COUNT
, FI.REPLY_COUNT
, FR.REPLY_MESSAGE
, TT.TECHNOLOGY_ID
, TT.TECHNOLOGY_NAME
FROM FORUM_TECHNOLOGY AS TT
INNER JOIN FORUM_QUESTION AS FQ
ON FQ.TECHNOLOGY_ID = TT.TECHNOLOGY_ID
LEFT OUTER JOIN (
SELECT QUESTION_ID
, COUNT(*) AS REPLY_COUNT
, MAX(DATE_REPLIED) AS DATE_REPLIED
FROM FORUM_REPLIES
GROUP BY QUESTION_ID
) AS FI
ON FI.QUESTION_ID = FQ.QUESTION_ID
INNER JOIN FORUM_REPLIES AS FR
ON FR.QUESTION_ID = FI.QUESTION_ID
AND FR.DATE_REPLIED = FI.DATE_REPLIED

Combine 2 SQL queries in 1 table

I have 2 Queries :
Query 1 :
SELECT w2.Avis,w2.Cde_Sap,w2.Don_Ordre,w2.PN_in ,w2.SN_in,w2.DATE2,w2.Statut_Cde,client.Zone
FROM w2
INNER JOIN client
ON w2.Don_Ordre = client.Client
WHERE client.Zone='CLR'
SORT BY w2.Avis ;
Query 2 :
SELECT w3.Avis,w3.Cde_Sap,w3.Don_Ordre,w3.PN_in , w3.SN_in,w3.DATE2,w3.Statut_Cde,client.Zone
FROM w3
INNER JOIN client
ON w3.Don_Ordre = client.Client
WHERE client.Zone='CLR'
SORT BY w3.Avis;
I want to display these 2 queries in 1 Page and SORT BY Avis . Its not working .
UNION ALL, in a derived table. ORDER BY on the result:
select * from
(
SELECT w2.Avis as Avis,w2.Cde_Sap,w2.Don_Ordre,w2.PN_in ,w2.SN_in,w2.DATE2,w2.Statut_Cde,client.Zone
FROM w2
INNER JOIN client
ON w2.Don_Ordre = client.Client
WHERE client.Zone='CLR'
UNION ALL
SELECT w3.Avis,w3.Cde_Sap,w3.Don_Ordre,w3.PN_in , w3.SN_in,w3.DATE2,w3.Statut_Cde,client.Zone
FROM w3
INNER JOIN client
ON w3.Don_Ordre = client.Client
WHERE client.Zone='CLR'
) dt
order by avis
Alternatively:
SELECT w2.Avis,w2.Cde_Sap,w2.Don_Ordre,w2.PN_in,w2.SN_in,w2.DATE2,w2.Statut_Cde,client.Zone
FROM (select * from w2
UNION ALL
select * from w3) as w2
INNER JOIN client
ON w2.Don_Ordre = client.Client
WHERE client.Zone='CLR'
SORT BY w2.Avis ;