Linq select different column into one - linq-to-sql

I have 3 table like
Person
--------
personname
student
-------
studentid
address
personid
teacher
--------
teacherid
address
personid
and my linq query is
var p=from p in db.person
join s in db.student
on p.personid equals s.personid
join t in db.teacher
on p.personid equals t.personid
select {address=????};
How can i select both student and teacher address into one field????

select new { address = s.address + " " + t.address };
This should work - change the " " with whatever separator you like.

Related

my sql output the same data more than one time when i use select statement

I created three tables "student" and "course" and "lecturer" and I inserted data into them.
Now I want to retrieve some data by select.
When I want to show: Subject taken by Kumar
SELECT STUDENT.NAME, COURSE.SUBJECT1, COURSE.SUBJECT2, COURSE.SUBJECT3
FROM STUDENT,COURSE
WHERE STUDENT.COURSE = COURSE.COURSE = 'MLVK'
it repeats the data more than one time.
I hope anyone help me
All the best
Learn to use proper, explicit, standard JOIN syntax.
And, table aliases:
SELECT s.NAME, c.SUBJECT1, c.SUBJECT2, c.SUBJECT3
FROM STUDENT s JOIN
COURSE c
ON s.COURSE = c.COURSE
WHERE c.COURSE = 'MLVK'
If I were going to use these tables to pick subjects taken by Kumar, I would write something like:
SELECT
s.name, c.course, c.subject1, c.subject2, c.subject3
FROM
student as s
LEFT JOIN course as c on c.course = s.course
WHERE
s.no_matrik = '23456'
GROUP BY
s.name, c.course, c.subject1, c.subject2, c.subject3
I think this version makes the intent slightly more clear (pick subject for a particular student, Kumar) than the previous answer (select subjects for any student having course = 'MLVK'). This answer will also return information for Kumar even if he has no course value in the Student table (pre-enrollment?).
First of all I would suggest that to model your data properly in BCNF form, where you should have modelled another table to persist subjects and map the lecturer who take that subject.
Ex: (Subject Table)
SubjectId LECT_ID
---------- ------------
TT234 L123
TT235 L003
and, your Course table would be more of course to subject mapping, like:
CourseName SubjectId
------------- --------------
DTM TT235
DTM TT695
...
then you use query as:
Select sub.SubjectId, l.NAME
From
Student s JOIN
Course c
on c.CourseName = s.COURSE
JOIN Subject sub
on sub.SubjectId = c.SubjectId
JOIN Lecturer l
on l.LECT_ID = sub.Lecturer
Where s.NAME = 'Aminah'
the above query will result as:
SubjectId NAME
--------- ----------
PP563 Ahmad
SS003 Ahmad
PP999 John
as Ahmad happens to be teaching 2 subjects in course DPG. but if you wish to report distinct Lecturers for Aminah, you can change query as:
SELECT NAME FROM Lecturer where LECT_ID
in (
Select l.LET_ID
From
Student s JOIN
Course c
on c.CourseName = s.COURSE
JOIN Subject sub
on sub.SubjectId = c.SubjectId
JOIN Lecturer l
on l.LECT_ID = sub.LECT_ID
Where s.NAME = 'Aminah'
) a

How can I join three SQL with two table having the same field ne

I have three tables
Student
-------------
id,
matno,
Surname,
faculty_id,
dept_id,
....
Faculty
------------
id,
name,
...
department
-------------------
id,
name,
....
I want to select student details using matno plus the name of faculty and name of department using faculty_id and dept_id . I have tried all SQL JOIN combinations i can think of and I only get one name field.
Also I get null on second query if I try to fetch query using different statement.
Edited
My SQL query is
SELECT * FROM student INNER JOIN faculty ON faculty.id = student.faculty_id LEFT JOIN department ON department.id = student.dept_id WHERE student.matno = 1104
Assuming that faculty_id and dept_id are foreign keys into the Faculty and department tables, this query should give you your desired result (replace ??? with the desired search value of matno):
SELECT s.matno, s.surname, f.name AS faculty, d.name AS department
FROM Student s
JOIN Faculty f ON f.id = s.faculty_id
JOIN department d ON d.id = s.dept_id
WHERE matno = ???
SELECT * FROM
STUDENT S
LEFT JOIN FACULTY F
ON S.FACULTY_ID = F.ID
LEFT JOIN DEPARTMENT D
ON S.DEPT_ID = D.ID WHERE S.MATNO = "$your_matno";
Try using this left join so that at least all students will be in the result set and select the required fields from it.

I want to count the CourseName like(4,2,5) in "Numbers Of Courses" column, but the output is being displayed full courseName

I want to display the full names of faculty members and the number of courses they are currently teaching.
My query is:
SELECT CONCAT( FName, ' ', LName ) AS "Faculty Names", course.CourseName AS 'NUmbers Of Courses'
FROM Faculty LEFT JOIN course ON (course.FacultyID = faculty.FacultyID)
ORDER BY faculty.FacultyID`
Question 1: I want to count the CourseName like(4,2,5) in the "Numbers Of Courses" column, but the output being displayed is the full courseName.
Question 2: Using a left join in the "Numbers Of Courses" column, some rows have Null values, but I want to show 0 there, instead.
You need a GROUP BY and COUNT. Check this -
SELECT CONCAT( FName, ' ', LName ) AS "Faculty Names",
COUNT(course.CourseName) AS 'NUmbers Of Courses', faculty.FacultyID
FROM Faculty
LEFT JOIN course ON (course.FacultyID = faculty.FacultyID)
GROUP BY faculty.FacultyID, "Faculty Names"
ORDER BY faculty.FacultyID
Note : You should include FacultyID while GROUP BY, because, you never can be sure that you will not have same FName + LName for any 2 Facultes.

How to change a record for all IDs, JOIN and concatenate

To clarify, my question that I formulated is, "Change the emails for all members of Sonata lastname#sonata.org, where lastname is the member's actual last name".
There are three tables in question. The first two are Members and the second is Artists, they are related using the xRefArtistsMembers. They are as follows:
Members
-------
MemberID, Firstname, Lastname, Address, Email
Artists
-------
ArtistID, ArtistName, City, Region, WebAddress
XrefArtistsMembers
------------------
ArtistID, MemberID
I formulated the following query:
alter a.*
from artists a
left join xrefartistsmembers x on x.artistid = a.artistid
left join members m on m.memberid = x.memberid
and m.email = m.lastname + ' % #sonata.org';
Unfortunately, it does not compile. Again, my question that I conjured to yeild my concept is, How to change the emails for all members of Sonata lastname#sonata.org, where lastname is the member's actual last name.
UPDATE members m
JOIN xrefartistsmembers x ON x.memberid = x.memberid
JOIN artists a ON x.artistid = a.artistid
SET m.email = whatever
WHERE m.email = CONCAT(m.lastname, '#sonata.org')
I'm not sure what the point of the JOIN is. Maybe this simpler query is enough:
UPDATE members m
SET m.email = whatever
WHERE m.email = CONCAT(m.lastname, '#sonata.org')
For any database
UPDATE Members SET Email='new email'
WHERE Email = Lastname + '#sonata.org'
AND MemberID IN (SELECT MemberID FROM XrefArtistsMembers)

How to fetch a particular record from three tables

Table Name :: Feedback_master
Fields 1. feed_id 2. roll_id 3. batch_id 4. sem_id (semester ID) 5.f_id (faculty Id) 6. sub_id (subject Id) 7. remark. 8. b_id
Table Name :: subject_master
Fields
sub_id (subject Id)
sub_name (Subject Name0
f_id ( Faculty ID)
Table Name :: faculty_master
Fields
f_id (Faculty Id)
f_name (Faculty Name)
l_name (Faculty Name)
b_id
This are the three tables. Now I want to fetch the detail from this three table.
I want the output as
f_Name (faculty name), Sub_name (Subject Name ) , and remark (Remark ) when i give the (faculty id) f_id
could some one help me to over come this problem.
Using Objects
Select T1.f_name, T2.sub_name, T3.remark from faculty_master as T1,
subject_master as T2, Feedback_master as T3 where T1.f_id = 'your faculty_id'
and T1.f_id = T3.f_id and T2.sub_id = T3.sub_id
heu, MySQL I presume?
SELECT f_name, sub_name, remark
FROM faculty_master
LEFT JOIN subject_master USING(f_id)
LEFT JOIN Feedback_master USING(f_id)
WHERE f_id = the_id_you_want
select fm.f_name, sm.sub_name, remark from faculty_master fm left
join sub_master sm on fm.f_id=sm.f_id
left join feedback_master fbm on
sm.sub_id = fbm.sub_id
where fm.f_id= 123
You can build up the query in stages. The first thing is that you're after a list of feedback remarks, so start with this simple select query:
SELECT * FROM Feedback_master
That's listing all the feedback from all over, but you want to limit it to only feedback on a particular faculty, so let's add a Where clause:
SELECT * FROM Feedback_master
WHERE Feedback_master.f_id = #f_id
Now we've got the right list of records, but the list of fields is wrong. You want the faculty name and subject name, which aren't there in the Feedback_master table; the subject_master and faculty_master tables are linked and assuming that every remark has a subject ID and a faculty ID, we can use a simple inner join to link the tables:
SELECT * FROM Feedback_master
INNER JOIN subject_master ON Feedback_master.sub_id = subject_master.sub_id
INNER JOIN faculty_master ON Feedback_master.f_id = faculty_master.f_id
WHERE Feedback_master.f_id = #f_id
Now it's pulling out all the fields from all three table; this includes all the fields we need, so we can now simply name them in the Select clause:
SELECT
faculty_master.f_name, subject_master.sub_name, Feeback_master.remark
FROM Feedback_master
INNER JOIN subject_master ON Feedback_master.sub_id = subject_master.sub_id
INNER JOIN faculty_master ON Feedback_master.f_id = faculty_master.f_id
WHERE Feedback_master.f_id = #f_id