comparing different columns and rows on the same table - php

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.

Related

How can I select this joined list in MySQL

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

Compare row result and create new column

I will like to compare row results and create a new column when a specific column have the same fightID.
My query:
SELECT fight.fightdato, fight.fightID, players.name,
playersfight.playerid, playersfight.playerScore
FROM fight
INNER JOIN playersfight ON fight.fightID = playersfight.fightID
INNER JOIN players ON playersfight.playerID = players.playersID
Result in php:
Fight Fight Date Player PlayerScore
1 2014-10-10 Kevin 8
1 2014-10-10 Chris 4
2 2014-09-01 Kevin 8
2 2014-09-01 Eric 4
My wanted result:
Fight Fight Date Player1 Result1 Player2 Result2
1 2014-10-10 Kevin 8 Chris 4
2 2014-09-01 Kevin 8 Eric 4
btw - I am new to php/mysql and programming in general
Thanks for helping me!
You need to join player and playerfight tables once more.
Something like:
SELECT fight.fightID, fight.fightdato,
p1.name as 'Player_1', pf1.playerid as 'PlayerID_1', pf1.playerScore as 'Result_1',
p2.name as 'Player_2', pf2.playerid as 'PlayerID_2', pf2.playerScore as 'Result_2'
FROM fight
JOIN playersfight pf1 ON fight.fightID = pf1.fightID
JOIN players p1 ON pf1.playerID = p1.playersID
JOIN playersfight pf2 ON fight.fightID = pf2.fightID
JOIN players p2 ON pf2.playerID = p2.playersID
WHERE p1.playersID < p2.players2
The WHERE clause with p1.playersID < p2.players2 condition excludes two cases that we don't want to have returned:
results with joined the same player twice in one row.
duplicates - for example for players with IDs 1 and 2 there would be returned the same fight twice with player IDs (1,2) and (2,1). Now it will return only first permutation.

Return all the rows even the joined table has empty results

In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .

mySQL select with PHP - left join

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

MySQL selecting row for attendance case

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)

Categories