I have 2 table.
t_family
ID Employee
A1 John
A2 Gladys
t_sibling
ID Name Status
A1 Darco Brother
A1 Carmen Sister
A1 Clara Sister
A2 Luther Brother
I'd like to make a list by selecting Employee and Name column.
SELECT (this code i'm looking for) AS Family, Status from t_family INNER JOIN t_sibling ON t_family.ID = t_sibling.ID
The output
Family Status
John Employee
Darco Brother
Carmen Sister
Clara Sister
Gladys Employee
Luther Brother
Is it possible? Thanks in advance.
Note:
can I do it without union? just a join. I have speed issue with union. Especially the data will be hundred thousands
use union to get your desired output from both table like below
select Employee as Name,status
from
(
select id,Employee ,'Employee 'as status from t1
union all
select id,Name,status from t2
)
order by id asc
Related
I'm having an issue with an sql query and I'm not sure what I am doing wrong. Anyways let me explain:
Initially this was the original query:
SELECT cl.*,
c.id,c.type,
c.firstname,
c.surname,
c.job,
c.company,
c.directorycompany_id,
dc.id, dc.name,
es.id FROM contactlist_contact cl
INNER JOIN contact c ON cl.contact_id = c.id
LEFT JOIN directorycompany dc ON dc.id = c.directorycompany_id
LEFT JOIN expertsection es ON es.id = c.expertsection_id
WHERE cl.contactlist_id = 36311
ORDER BY dc.surname
The statement fetches all of the details from the contactlist table where the id is X. The information it returns is a row for each contact in the contactlist table along with information on the company (directorycompany) they work for and various other details about the contact from the contact table. So the information looks something like this:
contactlist_id contact_id id active id type firstname surname job company directorycompany_id id name id
36311 1939 316955375 1 1939 directory Joe Bloggs Deputy Editor 786 786 Herald People 0
36311 1935 316955374 1 1935 directory Jim Bloggs Advertising Manager 786 786 Herald People 0
36311 28034 316955373 1 28034 directory Jay Bloggs News Reporter 786 786 Herald People 0
I then went and attempted to modify the above SQL as additional functionality was required but I've been seeing unwanted results. Basically I am trying to JOIN 3 other tables
directorycolumn
directorysupplement
directoryprogramme
The idea being that it would return all of the columns, supplements and programmes that the contact in the contactlist has also written. Also to point out, in some cases a contact may have written more than 1 column, supplement or programme and as a result I ideally wanted to display this in the same row as the contact as opposed to duplicating the rows so I used the GROUP_CONCAT() function.
This is the modified SQL
SELECT cl.*,
c.id,
c.type,
c.firstname,
c.surname,
c.job,
c.company,
c.directorycompany_id,
dc.id, dc.name,
es.id,
GROUP_CONCAT(dirc.name) AS gcname,
GROUP_CONCAT(dirp.name) AS gpname,
GROUP_CONCAT(dirs.name) AS gsname
FROM contactlist_contact cl
INNER JOIN contact c ON cl.contact_id = c.id
LEFT JOIN directorycompany dc ON dc.id = c.directorycompany_id
LEFT JOIN expertsection es ON es.id = c.expertsection_id
LEFT JOIN directorycolumn dirc ON dirc.directorycontact_id = c.id
LEFT JOIN directoryprogramme dirp ON dirp.directorycontact_id = c.id
LEFT JOIN directorysupplement dirs ON dirs.directorycontact_id = c.id
WHERE cl.contactlist_id = 36311
ORDER BY dc.surname
This returns:
contactlist_id contact_id id active id type firstname surname job company directorycompany_id id name id gcname gpname gsname
36311 28034 316955373 1 28034 directory Jay Bloggs News Reporter 786 786 Herald People 0 The Arts Scene,Farming \N \N
So my question is, where have the other 2 results gone and why are they not showing? And also why is the information in gcname being displayed for this contact when in fact it is related to the contact with the id 1939
if you remove GROUP_CONCAT it would display correct records, because when you use this function you should have GROUP BY clause. Currently it will consider all records as a single group.
If you look values in gcname is multiple, which is correct.
Group_concat is part of mysql aggregate functions. That means it will group all equal values together into one row, in your case all three columns have the same value, thats why you only get one as result. what result would you expect using group_concat?
We are making a simple database for our school programme. we have a table like this:
ID A B
1 John Stacey
2 Stacey Mark
3 Candice Rick
4 Stacey Rick
5 Rick Stacey
If we input Stacey's name, we display the number of rows where:
for every row (say, x and y)
rowX-ColumnA = rowY-ColumnB AND rowY-ColumnA = rowX-ColumnB
in this case, it should output:
2 rows:
4 Stacey Rick
5 Rick Stacey
Thanks! We have more than 100 students so we won't be able to do this manually. Again, I appreciate any hint.
Try this query my friend:
select t1.* from table1 t1
inner join table1 t2 on t1.A = t2.B and t1.B = t2.A;
SQL Fiddle
This will do it
SELECT p1.*
FROM People p1
INNER JOIN
People p2 ON p1.A=p2.B AND p1.B=p2.A
WHERE p1.A='Stacey' OR p1.B='Stacey'
See this SQLfiddle (SQL Server 2008)
Based on your example. I think you're probably looking for something like this:
select stu1.* from students stu1,
students stu2
where stu1.last_name = stu2.first_name
union
select stu1.* from students stu1,
students stu2
where stu1.first_name = stu2.last_name;
http://sqlfiddle.com/#!4/e9bbf/2/0
Above answer was based on your example of John Stacy , Stacy Rick.
I have two tables 'all' and 'jdetails'. I have an existing select query on the all table which works. I want to add some additional data from jdetails table if available.
all table:
judge, year, ...
jane doe, 2012
john doe, 2011
jdetails table:
name, designation,...
jane doe, level 1
jane doe, level 5
john doe, special
How do I change my query below to include the 'designation's (from jdetails) for each judge (in all)?
I think a left join is the solution but I have the where clause to consider. Also, I absolutely must have the results of this query below, but with added data from jdetails table if it exists.
Additionally, there can be multiple rows (jdetails.name) of designations for each all.judge which I want listed as a single value. e.g.-- jane doe would have designation value of 'level 1 level 5'.
I would join on all.judge=jdetails.name
current query:
$rows = $my->get_row("SELECT all.judge, `year`, `totlevel_avg`, `totlevel_count`, `genrank`, `poprank`, `tlevel_avg`, `tlevel_count`, `1level_avg` as `onelevel_avg`, `1level_count` as `onelevel_count`, `2level_avg` as `twolevel_avg`, `2level_count` as `twolevel_count`, `3level_avg` as `threelevel_avg`, `3level_count` as `threelevel_count`, `4level_avg` as `fourlevel_avg`, `4level_count` as `fourlevel_count`, `PSGlevel_avg`, `PSGlevel_count`, `I1level_avg`, `I1level_count`, `I2level_avg`, `I2level_count`, `GPlevel_avg`, `GPlevel_count`, `states` from `all` where `id` ='{$term}'");
any help is greatly appreciated.
I did not include everything you had in your SELECT statement, I just summarized it with ams.*. But the following links the all table to the jdetails table and then groups the designation into one field. Then I wrap the results in an outer query that pulls the rest of the fields you need in the all table (SQL Fiddle):
SELECT ams.*, am.Desigs
FROM
(
SELECT a.judge, GROUP_CONCAT(j.designation SEPARATOR ', ') AS Desigs
FROM `all` AS a
INNER JOIN jdetails AS j ON a.judge = j.name
GROUP BY a.judge
) AS am
INNER JOIN `all` AS ams ON am.judge = ams.judge
Here is an example that can help you:
`SELECT table1.column_name(s),table2.column_name(s) FROM table1
LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name
AND table1.column_name='Parameter'`
Where table 1 is all and table 2 is jdetails
I have these tables:
table 1 : attendance
-------------------------------
ID | DATE | EMPLOYEE_ID |
-------------------------------
1 2013-09-10 1
2 2013-09-10 2
3 2013-09-10 3
-------------------------------
table 2: employee
---------------
ID | NAME |
---------------
1 Smith
2 John
3 Mark
4 Kyle
5 Susan
6 Jim
---------------
My actual code to show employee option.
while($row=mysql_fetch_array($query)){
echo "<option value='$row[employee_id]'>$row[first_name] $row[last_name]</option>";
}
?>
How can i show the list of employee that not registered in table 1?
The condition is if an employee already registered in table 1, they won't appear on the option.
I want to show the list in <option>'s of <select> element. So it will return: kyle, susan, jim.
Please tell me the correct query or if there is any better option, it'll be good too. Please give some solution and explain. Thank you very much
UPDATE / EDIT:
it also based on current date, if in table 1 have no latest date e.g. today it's 2013-09-15. It will show all of employee.
You can do this with a left join and then checking for no matches:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id
where a.employee_id is null;
This is probably the most efficient option in MySQL.
EDIT:
To include a particular date, add the condition to the on clause:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id and a.date = date('2013-09-20')
where a.employee_id is null;
If I understood correctly this should work, get all employees whose ID is not in the attendance table.
SELECT * FROM employee WHERE employee.ID NOT IN
(SELECT EMPLOYEE_ID FROM attendance)
I have two tables, one which stores the registered students, lets call it students_register.
The second table keeps examination details of the students, lets call it exam_details.
In the students_register table, i store:
student registration number
first name
last name
email_address
date_of_birth
and other details.
In the exam_details table, i store the registration number and the students marks in the different subjects.
Now, the challenge is that i want to query the exam_details table and display the data in a table but instead of displaying the students registration number, i want to associate the registration number in the exam_details table to that in the students_register table so that i can display the student's names instead of the registration number.
How can i go about this?
1. students_register table
id reg_number first_name last_name email_address dob
1 P2894 John Smith john#example.com 12/05/1990
2. exam-details table
id reg_number english maths chemistry biology physics
1 P2894 60% 80% 50% 72% 64%
How do i display this data in a table such that i have
first_name last_name english maths chemistry biology physics
John Smith 60% 80% 50% 72% 64%
SELECT tb2.first_name, tb2.last_name, tb1.english, tb1.maths, tb1.chemistry, tb1.bilogy, tb1.physics
FROM exam_details AS tb1
INNER JOIN students_register AS tb2
ON tb1.reg_number = tb2.reg_number
Take a look at SQL Joins -> http://www.w3schools.com/sql/sql_join.asp
With a simple JOIN query:
SELECT id, student_register.reg_number, first_name,
last_name, english, maths, chemistry, etc
FROM student_register
JOIN exam-details ON student_register.reg_number = exam-details.reg_number
There is no need to use JOIN here. I think this is the easiest way.
SELECT
sr.first_name,
sr.last_name,
ed.english,
ed.maths,
ed.chemistry,
ed.biology,
ed.physics
FROM
students_register sr,
exam_details ed
WHERE
sr.reg_number = ed.reg_numver
See for JOIN.
SELECT first_name, last_name, english, maths, chemistry, biology, physics FROM exam_details AS ex JOIN students_register st ON (ex.reg_number = st.reg_number)
SELECT * FROM student-register INNER JOIN exam-details ON student-detail.reg_number=exam-details.reg_number
may be your query. Then you print out whatever you like.
select first_name, last_name, english, maths, chemistry, biology, physics
from students_register, exam_details
where students_register.reg_number=exam_details.reg_number
You have to join these 2 tables
SELECT s.first_name, s.last_name, e.english, e.maths, e.chemistry; e.biology, e.physics FROM students_register s LEFT JOIN exam_details e USING (reg_number)
May be this will help u.
select students_register.first_name,students_register.last_name,exam-details.english, exam-details.maths, exam-details.chemistry, exam-details.biology, exam-details.physics from exam-details left join students_register on students_register.reg_number=exam-details.reg_number