I only want the SELECT DISTINCT statement on 'yearLevel' (as I don't want any duplicates only on yearLevel)
image of data displayed on screen
For example I only want one 'yearLevel' 12 record in the table, and for that to be the one with the lowest 'totalSeconds'
code
$sql = "SELECT firstName, lastName, yearLevel, totalSeconds
FROM studentInformation
JOIN record
ON studentInformation.studentId = record.student_Id
ORDER BY totalSeconds ASC
LIMIT 1 ";
Is it possible to do this -
$sql = "SELECT firstName, lastName, DISTINCT yearLevel, totalSeconds
FROM studentInformation
JOIN record
ON studentInformation.studentId = record.student_Id
ORDER BY totalSeconds ASC
LIMIT 1 ";
Provided that "firstName" and "lastName" are in table "studentInformation", and "yearLevel" as well as "totalSeconds" are in table "record" this query should be working. It uses a correlated subquery. I did not test this; if it doesn't work please let me know.
SELECT a.firstName, a.lastName, b.yearLevel, b.totalSeconds
FROM studentInformation a
INNER JOIN record b ON a.studentId = b.studentId
WHERE b.totalSeconds = ( SELECT min(totalSeconds)
FROM record
WHERE yearLevel = b.yearLevel )
ORDER BY b.totalSeconds ASC
Assuming that only "totalSeconds" is in table "record" this could work, too.
SELECT a.firstName, a.lastName, a.yearLevel, b.totalSeconds
FROM studentInformation a
INNER JOIN record b ON a.studentId = b.studentId
WHERE b.totalSeconds = ( SELECT MIN(d.totalSeconds)
FROM studentInformation c
INNER JOIN record d ON c.studentId = d.studentId
WHERE c.yearLevel = a.yearLevel )
ORDER BY b.totalSeconds ASC
Related
In my query i get the first record i olje table when i group by frl_nr. There are many records with the same frl_nr i the olje table. I want to get the latest record from the olje table by date column. Her is my search string:
$sql = "SELECT *
FROM frl_sok
INNER JOIN olje ON frl_sok.id_nr = olje.id_nr
WHERE frl_sok.kunde_nr = '$kunde'
AND frl_sok.jobb_nr = '$jobb_nr'
GROUP BY frl_sok.frl_nr DESC";
What is the solution?
Both of my tables are here
You should use row_number() rather than trying to group by.
Try this:
select * from (
SELECT *,
row_number() over (partion by frl_sok.frl_nr order by date desc) rn
FROM frl_sok
INNER JOIN olje ON frl_sok.id_nr = olje.id_nr
WHERE frl_sok.kunde_nr = '$kunde'
AND frl_sok.jobb_nr = '$jobb_nr'
) q where rn=1"
Here is the solution I came up with and it works.
$sql = "SELECT a.id_nr, a.frl_nr, a.maskin_id, a.tilstand_olje, b.date FROM frl_sok a INNER JOIN olje b ON a.id_nr = b.id_nr INNER JOIN (SELECT id_nr, MAX(date) Max_Date FROM olje GROUP BY id_nr) c ON b.id_nr = c.id_nr AND b.date = c.Max_date WHERE a.kunde_nr = '$kunde' AND a.jobb_nr = '$jobb_nr'";
I have a table_resident like in image below
And then i have a table_complaints like image below
with the table_resident.resident_id = table_complaints.resident_id and also table_resident.resident_id = table_complaints.party_id
How can i join resident_id = 1 and party_id = 2 name in same row like image below? I can see resident_id = 1 first_name, last_name but in resident_id = 2 how can i see them again by using party_id?
Here is my code in joining but in party_id i dont know what i will use the firstname, lastname, middlename again
SELECT
table_involvement.*,
table_resident.first_name,
table_resident.last_name,
table_resident.middle_name,
table_complaints.*
FROM ((
table_complaints
LEFT JOIN table_resident
ON table_complaints.resident_id = table_resident.resident_id)
LEFT JOIN table_involvement
ON table_complaints.complaints_id = table_involvement.complaints_id)
ORDER BY table_resident.first_name ASC";
This should do it. I user 2 inner joins to achieve this solution.
SELECT TABLE_RESIDENT.FIRST_NAME, TABLE_RESIDENT.LAST_NAME,
TABLE_RESIDENT.MIDDLE_NAME, TABLE_COMPLAINTS.*, TABLE_INVOLVEMENT.*
FROM TABLE_RESIDENT
INNER JOIN TABLE_COMPLAINTS
ON TABLE_COMPLAINTS.RESIDENT_ID = TABLE_RESIDENT.RESIDENT_ID
INNER JOIN TABLE_INVOLVEMENT
ON TABLE_COMPLAINTS.RESIDENT_ID = TABLE_INVOLVEMENT.COMPLAINTS_ID
WHERE TABLE_COMPLAINTS.PARTY_ID = 2
ORDER BY TABLE_RESIDENT.FIRST_NAME ASC
I'm trying to create a leaderboard but i'm not sure how to do the mysql query.
I would like to count all the levels from a player in the skills table and get the total Level and count all the experience from a player in the experience table and get the Total Exp along with displaying the persons name from the users column.
There is 3 tables factions_mcmmo_users, factions_mcmmo_experience, factions_mcmmo_skills.
This is what i have so far but it doesn't work:
$sql = ("SELECT a.id,
(SELECT COUNT(*) FROM factions_mcmmo_experience WHERE user_id = a.id) as TotalXP,
(SELECT COUNT(*) FROM factions_mcmmo_skills WHERE user_id = a.id) as TotalLevel
FROM (SELECT DISTINCT id FROM factions_mcmmo_users) a LIMIT 10;");
Any help would be very appreciated
EDIT: I have it working now but i'm unsure if its the most efficient way to do things so if anyone could help me out if theres a better way, it would mean a lot.
I would also like to know if it's possible to display the total exp and level with commas if the number is in the thousands for example: total level 5,882 and total xp 582,882
EDIT 2:
I have figured out how to format the numbers but still don't know if my code is efficient
$sql = ("SELECT id, user,
(SELECT FORMAT(Sum(taming)+Sum(mining)+Sum(woodcutting)+Sum(repair)+Sum(unarmed)+Sum(herbalism)+Sum(excavation)+Sum(archery)+Sum(swords)+Sum(axes)+Sum(acrobatics)+Sum(fishing)+Sum(alchemy),0) FROM factions_mcmmo_skills b WHERE b.user_id = a.id) as TotalLevel,
(SELECT FORMAT(Sum(taming)+Sum(mining)+Sum(woodcutting)+Sum(repair)+Sum(unarmed)+Sum(herbalism)+Sum(excavation)+Sum(archery)+Sum(swords)+Sum(axes)+Sum(acrobatics)+Sum(fishing)+Sum(alchemy),0) FROM factions_mcmmo_experience c WHERE c.user_id = a.id) as TotalXP
FROM (SELECT id, user FROM factions_mcmmo_users) a group by id ORDER BY TotalLevel DESC, TotalXP DESC LIMIT 10;");
EDIT 3
Updated code from scaisEdge but was displaying everyones level as 1 and XP as 1, so i changed count(*) changed to sum, added an order By TotalLevel in Descending order and that seems to have worked but i can't get it to display the persons name (user column) in the user table? not sure if i was supposed to change to sum because it didn't work the other way.
$sql = ("SELECT a.id, b.TotalXP, c.TotalLevel
FROM (SELECT DISTINCT id FROM factions_mcmmo_users) a
INNER JOIN (
SELECT user_id, Sum(taming)+Sum(mining)+Sum(woodcutting)+Sum(repair)+Sum(unarmed)+Sum(herbalism)+Sum(excavation)+Sum(archery)+Sum(swords)+Sum(axes)+Sum(acrobatics)+Sum(fishing)+Sum(alchemy) as TotalXP
FROM factions_mcmmo_experience
GROUP By user_id
) b on b.user_id = a.id
INNER JOIN (
SELECT user_id, Sum(taming)+Sum(mining)+Sum(woodcutting)+Sum(repair)+Sum(unarmed)+Sum(herbalism)+Sum(excavation)+Sum(archery)+Sum(swords)+Sum(axes)+Sum(acrobatics)+Sum(fishing)+Sum(alchemy) as TotalLevel
FROM factions_mcmmo_skills
GROUP by user_id
) c on c.user_id = a.id
ORDER BY TotalLevel DESC
LIMIT 10;");
EDIT 4
Everything working but when i try to format the totals using "FORMAT(Sum(Columns), 0) on the inner joins, the EXP Total appears to work but the main Total Level is not displaying results that are over 1,000 and it breaks the leaderboard positioning, it should be sorting them on total level but it appears to be random, when u remove the format,0 it goes back to working
I would like it to display commas if the number number is the thousands for example: Total Level: 5,532 and Total EXP 5882,882
See live demo: http://mcbuffalo.com/playground/leaderboards/server/factions-mcmmo.php
Updated Code trying to use Format:
$sql = ("SELECT a.id, a.user, b.TotalXP, c.TotalLevel
FROM (SELECT id, user FROM factions_mcmmo_users) a
INNER JOIN (
SELECT user_id, FORMAT(Sum(taming)+Sum(mining)+Sum(woodcutting)+Sum(repair)+Sum(unarmed)+Sum(herbalism)+Sum(excavation)+Sum(archery)+Sum(swords)+Sum(axes)+Sum(acrobatics)+Sum(fishing)+Sum(alchemy), 0) as TotalXP
FROM factions_mcmmo_experience
GROUP By user_id
) b on b.user_id = a.id
INNER JOIN (
SELECT user_id, FORMAT(Sum(taming)+Sum(mining)+Sum(woodcutting)+Sum(repair)+Sum(unarmed)+Sum(herbalism)+Sum(excavation)+Sum(archery)+Sum(swords)+Sum(axes)+Sum(acrobatics)+Sum(fishing)+Sum(alchemy), 0) as TotalLevel
FROM factions_mcmmo_skills
GROUP by user_id
) c on c.user_id = a.id
ORDER BY TotalLevel DESC;");
EDIT 5
Changed number with PHP, everything works
Original Images
you could use an couple of inner join
$sql = ("SELECT a.id, a.name, b.TotalXP, c.TotalLevel
FROM (SELECT DISTINCT id, name FROM factions_mcmmo_users) a
INNER JOIN (
SELECT user_id, COUNT(*) as TotalXP
FROM factions_mcmmo_experience
GROUP By user_id
) b on b.user_id = a.id
INNER JOIN (
SELECT user_id, COUNT(*) as TotalLevel
FROM factions_mcmmo_skills
GROUP by user_id
) c on c.user_id = a.id
LIMIT 10
I am stuck with a complex MySQL query. I have following tables:
[employee]
id
name
[department]
id
name
[employee_department_relation]
id
employee_id
department_id
[attendance]
id
employee_id
date
Note: attendance will be recorded only if the employee is present at that date. No specific flag is set for abseentism.
I need to find out that, how many employees from which department were absent on a particular date.
I have done so far, following SQL, but, its not perfect:
SELECT A.date,
(SELECT name FROM employee WHERE id = emp_id) AS employee,
(SELECT D.name AS dept_name
FROM deptartment D
INNER JOIN employee_department_relation R ON R.dept_id = D.id
INNER JOIN employee E ON E.id = R.emp_id
WHERE R.emp_id = A.emp_id
) AS dept
FROM attendance A
WHERE A.date = '2014-07-03'
How can I proceed?
try this changing date as you like:
select employee.name, department.name from employee
left join employee_department_relation r on r.employee_id = employee.id
left join department on department.id = r.department_id
where employee.id not in (
select employee_id from attendance where date = '2014-07-28'
)
I am struggling with a MYSQL query - I have 2 tables :
Table 1 (info) containing UID, first_name, last_name.
Table 2 (card) containing UID, pic .
What I am trying to do is get all results into an array:
WHERE UID IN '$ids' AND LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC
I figured an INNER JOIN so my current code is:
("SELECT UID, first_name, last_name, pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM info
INNER JOIN card ON info.UID=card.UID)
WHERE LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC")
This is producing the following error though:
'Every derived table must have it's own alias'.
Am I going about this the right way with inner join, and how do I give the derived table an alias? Thanks in advance!
select b.UID, g.first_name, g.last_name, b.pic
from user_data.general_info g
inner join user_data.Bcards b on g.UID = b.UID
where LEFT(g.last_name, 1) = '$letter'
order by g.last_name, g.first_name asc
The inner query should be named.
SELECT users.UID, users.first_name, users.last_name, users.pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM user_data.general_info
INNER JOIN user_data.Bcards ON general_info.UID=Bcards.UID) users
WHERE LEFT(users.last_name,1) = '$letter' ORDER BY users.last_name, users.first_name ASC