comparision of two table having same field? - mysql

I have to table.
Both table has nric field.
I want to select the nric field which does not inside in table.
si_isccourse table
enter code here
ID NRIC
1 456
2 457
3 458
si_results table
ID NRIC
1 456
si_isc_class table
ID NRIC
1 456
2 457
my results like this
ID NRIC
3 458
this is my sql query
SELECT DISTINCT(isc.isc_nric) from si_isccourse iscLEFT JOIN si_results re ON re.re_nric=isc.isc_nric LEFT JOIN si_isc_class cla ON isc.isc_nric!=cla.isc_class_nric WHERE (isc.isc_second_choice='FPS') AND ( re.re_year IN('2010','2009')) AND ( re.re_code IN('VETCA1','VETCA2')) AND isc.isc_nric!=cla.isc_class_nric ORDER BY re.re_mark desc
I want to get data not in si_isc_class table.
I want to select data from si_isccourse and compare with si_results and not in si_isc_class

SELECT first.*
FROM first
LEFT JOIN second ON(first.id = second.id)
WHERE second.id IS NULL

You might want to change the position of the tables.
some thing like
...
FROM si_isc_class ..
Left JOIN ....
Then you will get the desired value.

Related

Adding values in a column based on a query result

I have a table:---
id
name
dept
1
Alice
CS
2
Bob
Elect
3
David
Mech.
and a query result:-
id
count
1
100
2
22
3
50
Then I want to add the count column from the query to my original table, something like:-
id
name
dept
count
1
Alice
CSE
100
2
Bob
Elect
22
3
David
Mech.
50
The only I figured out to do, is by storing the query result into a new table and then using UPDATE...SET...WHERE. Is there any way to do it without creating a new table?
First you need to create the count column in tablename using
ALTER TABLE `tablename` ADD COLUMN `nr_count` INT;
Then use:
update tablename t
inner join ( SELECT id,
count(*) as nr_count
FROM tablename
GROUP BY id
) as t1
set t.nr_count=t1.nr_count ;

Get count of a column based on join from other table

I am new to SQL. I have to tables and trying to get the count of column from table 1 and join by other column in table 2.
Table 1:
credits | sec_code | student_acc_id
--------------------------------
4 TUB 2098
5 JIY 2099
6 THG 3011
Table 2:
id| sec_code | student_acc_id | stu_id
-------------------------------------
1 TUB 2098 1011
5 JIY 2099 1011
7 THG 3011 1012
I would like to get the sum of credits for the student by getting the stu_id from table2 from sec_code and get all the student_acc_id for stuId and sum the credits column in table1 for all the student account Ids found from table 2. I am not sure how can we join or make this query simple.
Normally my approach is to inlcude this two to three different SQL statements, but i am looking for this in a one sql query if possible.
For the above example lets say i want to get sum of credits for all student_acc_id where stu_id is 1011 from second table. i just have the first table. So the output should be 4+5 as both accounts belong to the same student.
So i need:
--> get the stu_id based on sec_code from table two (lets say for TUB sec_code)
--> get all student_acc_id from table where stu_id is result from above statement
-->now using those all student_acc_id's sum the credit in table 1
Any help is appreciated !
Use Join according to your Data exist.
But as per the sample data better to get Data from Right table.
select
tab2.stud_id,
SUM(tab1.credits) AS credit_sum
from
table1 as tab1
right join
table2 as tab2
on tab1.student_acc_id = tab2.student_acc_id
and tab1.sec_cd = tab2.sec_cd
where
tab2.stud_id in(
select distinct stud_id
from table2
where student_acc_id = '2098'
)
group by
tab2.stud_id;
You can check its sample output here. Please click for fiddle solution.
try like below
select t2.stu_id,
sum(t1.credits)
from t1 join t2 on t1.student_acc_id=t2.student_acc_id and
t1.sec_code=t2.sec_code
group by stu_id
Having count(stu_id)>1

How to get all records from one table and null with other table

Hi i have two tables event_service and appointment_feedback in which i have
two records in event_service table and one record in appointment_feedback
so i want matching record with event_service table and null record with
appointment_feedback.
Please follow my table structure
First table:event_service
....................................
evsr_id evsr_ev_id staff_id
....................................
1 101 1
2 101 2
Second table:appoitment_feedback
....................................
fd_id fd_app_id fd_comment
....................................
1 101 test
I want this output:
.................................................
evsr_id evsr_ev_id staff_id fd_comment
.................................................
1 101 1 test
2 101 2 null
I have tried below query but it is not giving null value it is giving repeeated value of comment i.e. test but it has only one entry in appointment_feedback table so i want two record from event_service table
and one record from appointment_feedback table with corresponding to that
record.
SELECT appointment_feedback.`fd_id`,`fd_app_id`,`fd_comment`,`fd_service_rat`
FROM event_service
left join `appointment_feedback`
on event_service.evsr_ev_id=appointment_feedback.fd_app_id
WHERE appointment_feedback.fd_app_id='3959'
This query is giving wrong Output:-
..............................................
fd_id fd_app_id fd_comment fd_service_rat
.............................................
1 101 test 0
1 101 test 0
I have used simple LEFT JOIN it give requested output.
DECLARE #event_service AS Table
(
evsr_id INT,
evsr_ev_id INT,
staff_id INT
)
DECLARE #appoitment_feedback AS Table
(
fd_id INT,
fd_app_id INT,
fd_comment VARCHAR(50)
)
INSERT INTO #event_service VALUES(1,101,1)
INSERT INTO #event_service VALUES(2,101,2)
INSERT INTO #appoitment_feedback VALUES(1,101,'test')
SELECT
es.*,
af.fd_comment
FROM #event_service es
LEFT JOIN #appoitment_feedback af ON es.evsr_ev_id=af.fd_app_id
AND es.evsr_id=af.fd_id
Output:
According to your question, you had the right tools but you miss-used them.
This is your current query:
SELECT appointment_feedback.`fd_id`,`fd_app_id`,`fd_comment`,`fd_service_rat` FROM `appointment_feedback` left join event_service on event_service.evsr_ev_id=appointment_feedback.fd_app_id WHERE appointment_feedback.fd_app_id='3959'
All what you have to do is swap the tables in a way to select from event_service and left join appointment_feedback, so this should be your new query:
SELECT appointment_feedback.`fd_id`,`fd_app_id`,`fd_comment`,`fd_service_rat`
FROM event_service
left join `appointment_feedback`
on event_service.evsr_ev_id=appointment_feedback.fd_app_id
WHERE appointment_feedback.fd_app_id='3959'
LEFT JOIN performs a join starting with the first (left-most) table and then any matching second (right-most) table records.
Source

Selecting data from two table with condition

I have two tables
1. studentprofile
sud_id name
1 kp
2 kishan
3 raj
2. fee_generate
fee_id stud_id fee_balance name
1 1 0 kp
2 2 10 kishan
I want to show those students whose fee is not submitted or have any balance, which means that I want to show is as following
kishan and raj
I am not able to write the query with any join. My second problem is that name columns are common in both tables.
So the selected name column should come from studentprofile table and order by name from studentprofile table.
You need to left join the fee_generate table on the student_profile table and include those records where the fee_generate.student_id is null (not submitted) or the balance is over 0:
select s.* from student_profile s
left join fee_generate f on s.stud_id=f.stud_id
where f.stud_id is null or f.balance>0
order by f.name

Select From table name obtained dynamically from the query

I have 3 Tables
campaign1 (TABLE)
id campaign_details
1 'some detail'
campaign2 (TABLE)
id campaign_details
1 'some other detail'
campaign_list (TABLE)
id campaign_table_name
1 'campaign1'
2 'campaign2'
Campaign list table contains the table name of the two tables described above. I want to Select from the Campaign List table and get the record count using the table name i get from this select
For eg.
using select i get campaign1(Table name). Then i run select query on campaign1 to count number of records.
What i'm doing right now is .
-Select from campign_list
-loop through all campaign_table_names and run select query individually
Is there a way to do this using a single query
something like this
select campaign_name,(SELECT COUNT(*) FROM c.campaign_name) as campcount from campaign_list c
SQLFiddle : http://sqlfiddle.com/#!9/b766d/2
It's not possible inside a single query to build it dynamically but it's possible to cheat. Especially if there are only two linked tables.
I've listed two options
left outer join both tables
select campaign_name,
coalesce(c1.campaign_details, c2.campaign_details)
from campaign_list c
left join campaign1 c1 using (id)
left join campaign2 c2 using (id);
union all two different selects
select campaign_name,
campaign_details
from campaign_list c
join campaign1 c1 using (id)
union all
select campaign_name,
campaign_details
from campaign_list c
join campaign2 c2 using (id);
sqlfiddle
Combine your campaign tables to 1 table and add an column named 'type' (int).
campaign_items tables:
item_id item_details item_type
1 'some detail' 1
2 'some detail' 1
3 'some other detail' 2
4 'some other detail' 2
campaign_lists table
campaign_id campaign_name
1 'campaign1'
2 'campaign2'
Then you can use the following select statement:
SELECT campaign_name, (SELECT COUNT(*) FROM campaign_items WHERE item_type = campaign_id) as campaign_count
FROM campaign_lists
Oops, writing took me so long that you got this answered by Colin Raaijmakers already. Well, I'll post my answer anyway in spite of being more or less the same answer. Maybe my elaboration helps you see the problem.
Your problem stems from a bad database design. A database is made to order data and its relations. A CD database holds albums, songs, artists, etc. A business database may hold items, warehouses, sales and so on. Your database holds table names. [... time for thinking :-) ]
(When writing a DBMS you would want to store table names, column names, constraints etc., but I guess I am right supposing that you are not writing a new DBMS.)
So create tables that deal with your actual data. E.g.:
campain_type (id_campain_type, description, ...)
campain (id_campain, id_campain_type, campain_date, ...)
campain_type
id_campain_type description
1 Type A
2 Type B
3 Type C
campain
id_campain id_campain_type date
33 1 2015-06-03
85 2 2015-10-23
97 2 2015-12-01
query
select
ct.description,
(select count(*) from campain c where c.id_campain_type = ct.id_campain_type) as cnt
from campain_type ct;
result
description cnt
Type A 1
Type B 2
Type C 0