Below are my tables:
1. tbluser
UserNumber - PK
Name
MemberType - Number
StationNumber - FK (connected to StationNo of tblStation)
2.tblStation
StationNo - PK
StationName
3.tblUserLogs
LogID - PK
UserID - FK (connected from UserNumber of tblusers)
LastLog
And all I want to do is to display Name (tblusers), StationName(tblStation) and LastLog(tblUserLogs) where MemberType is not equal to 1.
Here's my try...
SELECT tblusers.FirstName, tblstation.StationName, tblUserLogs.LastLog
FROM (tblstation INNER JOIN tblusers ON tblstation.StationNo = tblusers.StationNumber)
INNER JOIN tblUserLogs ON tblusers.UserNumber = tblUserLogs.UserID
WHERE (((tblusers.MemberType)<>1))
However, I get repeating records of my users. It displays ALL the LastLog data instead of showing the latest.
How should I do it?
Use this query:
SELECT tblusers.FirstName, tblstation.StationName, MAX(tblUserLogs.LastLog)
FROM (tblusers LEFT JOIN tblstation ON tblstation.StationNo = tblusers.StationNumber)
LEFT JOIN tblUserLogs ON tblusers.UserNumber = tblUserLogs.UserID
WHERE tblusers.MemberType != 1
Related
i have the following tables:
Technician
Tech_ID,First_Name,Last_Name
RT_QUEUE_Delta
Tech_ID, RT_Complete` (references a `Tech_ID` in `Technician`).
I need to get the data from a row in RT_Queue_Delta where RT_Completed = ?? but in my output I need to have the First_Name and Last_name that correlates with Tech_id and RT_Completed.
I can match one but I don't know how to match both. I tried:
select RTTech.First_Name as RT_First_Name,
RTTech.Last_Name as RT_Last_Name
from Technician as RTTech
Join RT_Queue_Delta as RT
on RT.RT_Completed = RTTech.Tech_ID
You can join to the Technician table multiple times:
select d.tech_id, t.first_name, t.last_name,
d.rt_completed as completed_id,
t2.first_name as completed_first_name,
t2.last_name as completed_last_name
from RT_QUEUE_Delta d
join Technician t on d.tech_id = t.tech_id
join Technician t2 on d.RT_Completed = t2.tech_id
ft_pupils
- id //Primary Key
- name
- start_date
ft_entries
- id
- pupil_id //Foreign Key
- aol_id
ft_aol
- id
- title
For every entry in the ft_entries table I want to use a SELECT to select every entry in the ft_entries table but with the aol_id replaced with the title inside of ft_aol.
I have managed to get:
SELECT name, aol_id FROM ft_pupils, ft_entries WHERE pupil_id = ft_pupils.id
to work fine.
I want the WHERE to be WHERE pupil_id = ft_pupils.id
I am so confused right now.
You can try this to get what you want by join 3 table.
SELECT Name,title FROM ft_entries
INNER JOIN ft_pupils ON pupil_id = ft_pupils.id
LEFT JOIN ft_aol ON ft_aol.id = aol_id
This will show you how to get the data you're looking for and display all the ids. You can remove the ids if you don't care to see them:
SELECT a.id,
b.pupil_id,
c.id,
a.NAME,
a.start_date,
c.title
FROM ft_pupils a
JOIN ft_entries b ON a.id = b.pupil_id
JOIN ft_aol c ON c.id = b.aol_id
I have a query that is giving me trouble, im not sure how to do this
I have to retrieve records from a TICKETS table and join it together with 2 others,, That is not the problem, i need to replace one of the values in the record for a value in another table....
For Eg:
Table tickets:
numint
user_id
type
desc
attach
priority
status
date
assignation
Table users
numint
company_id
name
email
username
password
ps
security
token
date
Table companies
numint
name
Example Record
company_name - user_name - type - title - priority - status - date - assignation
"someCompany" - "someUser" - "someTitle" - 1 - "open" - "yyy/mm/dd" - 2(user_id)
in the assignation field of the returned record i need to replace it with the correspondant value from the users table IF it's NOT 0 (zero)
This is my Query so far
SELECT tickets.numint, tickets.type, tickets.title, tickets.description,
tickets.priority, tickets.status, tickets.date,
users.name AS user_name, companies.name AS company_name,
CASE WHEN tickets.assignation=0 THEN 'not-assigned'
ELSE ????????? END AS assignation
FROM tickets
LEFT JOIN users ON tickets.user_id = users.numint
LEFT JOIN companies ON users.company_id = companies.numint
i dont know how to replace that value, or if the CASE should after the joins...
Do you want to show username as assignation. use users.name in case.
SELECT tickets.numint, tickets.type, tickets.title, tickets.description,
tickets.priority, tickets.status, tickets.date,
users.name AS user_name, companies.name AS company_name,
CASE WHEN tickets.assignation=0 THEN 'not-assigned'
ELSE users1.name END AS assignation
FROM tickets
LEFT JOIN users ON tickets.user_id = users.numint
LEFT JOIN companies ON users.company_id = companies.numint
LEFT JOIN users AS users1 ON tickets.assignation = users1.numint
I have two table in database.
1) tbl_lab_checkup
2) tbl_lab
tbl_lab has all the records of laboratories and tbl_lab_checkup has all the records o checkup that lab provides.
Following are the fields in tables
1) tbl_lab_checkup
-- labcheckupid (pk)
-- labid (fk)
-- checkupid (fk)
-- cost
-- discount
2) tbl_lab
-- labid (pk)
-- labname
-- labarea (fk)
I have 'areaid'=1 and 'checkupid' in array which I contact with "," (1,2).
What I want is to get all the lablist available in areaid=1 who provides all the checkup in an array (1,2)
I tried following query But Im getting wrong result.
SELECT tlc .* FROM tbl_lab_checkup tlc
INNER JOIN tbl_lab lb ON
tlc.labid = lb.labid
WHERE
tlc.checkupid IN (1,2) AND lb.labarea=1
GROUP BY lb.labid
It return result even if lab provides only one id in array. Anyone have solution for this.
Check fiddle : http://sqlfiddle.com/#!2/5c674/1
If you have building this query then you can add the having clause like following:
SELECT tlc .* FROM tbl_lab_checkup tlc
INNER JOIN tbl_lab lb ON
tlc.labid = lb.labid
WHERE
tlc.checkupid IN (1,2) AND lb.labarea=1
GROUP BY lb.labid
having count(tlc.checkupid) = 2;
Here having count(tlc.checkupid) = 2; the value 2 is the count of your elements in tlc.checkupid IN (1,2).
And if you'd like distinct tlc.checkupid, you could switch the having count(tlc.checkupid) = 2 to having count(distinct tlc.checkupid) = 2;
I would generally go with the suggestion above by vinodadhikary, but another option would be to use one JOIN per check:-
SELECT lb.*
FROM tbl_lab lb
INNER JOIN tbl_lab_checkup tlc1 ON tlc1.labid = lb.labid AND tlc1.checkupid = 1
INNER JOIN tbl_lab_checkup tlc2 ON tlc2.labid = lb.labid AND tlc2.checkupid = 2
WHERE lb.labarea=1
This will work when you want all the tbl_lab_checkup record details
User this code to get your desired result in CI:
$this->db->select("tbl_lab.*");
$this->db->join("tbl_lab_checkup","tbl_lab_checkup.labid = tbl_lab.labid");
$this->db->where("tbl_lab.labarea","1");
$find="FIND_IN_SET('1,2','tbl_lab_checkup.checkupid')";
$this->db->where($find);
$this->db->get("tbl_lab");
I'm working on a query that doesn't behave as expected, and would appreciate any help on pointing me in the right direction.
TABLES
I have three central tables consists of the following.
table_categories
- id (int)
- name (varchar)
table_events
- id (int)
- name (varchar)
- created (date time)
table_taxonomy
- cat_id (id from table_categories)
- event_id (id from table_events)
- type (varchar in this example always 'category')
GOAL
Count events created after a certain date in each category. The result should be something like:
COUNT NAME ID
3 Rock 1
2 Punk 3
QUERY
This is the query that I've come up with. The problem is that the result doesn't care about the created date, and grabs all events regardles of when they where created.
SELECT COUNT(*) AS count,
table_categories.name,
table_categories.id
FROM table_taxonomy
INNER JOIN table_categories
ON table_categories.id = table_taxonomy.cat_id
LEFT JOIN table_events
ON table_events.id = table_taxonomy.events_id
WHERE table_taxonomy.type = 'category'
AND table_taxonomy.cat_id IN(2,3)
AND table_events.created > '2012-10-07 05:30:00'
GROUP BY (table_categories.name)
try this one, just small change while comparing date :
SELECT COUNT(*) AS count,
table_categories.name,
table_categories.id
FROM table_taxonomy
INNER JOIN table_categories
ON table_categories.id = table_taxonomy.cat_id
LEFT JOIN table_events
ON table_events.id = table_taxonomy.events_id
WHERE table_taxonomy.type = 'category'
AND table_taxonomy.cat_id IN(2,3)
AND Date(table_events.created) > '2012-10-07 05:30:00'
GROUP BY (table_categories.name)
Try this one,
SELECT c.ID, c.Name, COUNT(*)
FROM table_categories a
INNER JOIN table_taxonomy b
ON a.id = b.cat_id
INNER JOIN table_events c
ON b.event_ID = c.ID
WHERE c.created > 'dateHere'
-- AND ..... -- other conditions here
GROUP BY c.ID, c.Name