Now I have 2 tables
STUDENT:
STUDENT_ID | STUDENT_NAME | COURSE_ID
1000 | Anson | 1
1001 | Jnson | 1
1002 | Andy | 2
1003 | Alex | 3
COURSE:
COUSE_ID | COURSE_NAME
1 | P5A
2 | P5B
3 | P5C
Now I would like to produce the result to show the students name in each class
Idea result:
P5A P5B P5C
Anson Andy Alex
Jason
what should I do, I am using php + mysql to build web system
select * from course c left join student s on c.course_id = s.course_id
order by c.course_name, c.course_id
Enumerate the result set. Each time course_id changes, start a new section.
Related
My database looks something like this, in order of user, userteam (connection table) and team:
Usertable UserTeam Teamtable
+--------+------+ +--------+--------+ +--------+-------+
| userid | name | | userid | teamid | | teamid | name |
+--------+------+ +--------+--------+ +--------+-------+
| 1 | eric | | 1 | 1 | | 1 | awsm |
| 2 | john | | 1 | 2 | | 2 | doe |
| 3 | carl | | 2 | 1 | | 3 | empty |
+--------+------+ | 3 | 1 | +--------+-------+
+--------+--------+
How do I select all users that IS NOT in a team, and echo them out only once? I've tried doing it with the usertable, but then it will echo for instance, if I try and select all members not in team 3:
SELECT userid FROM userteam WHERE teamid!=3;
SELECT * FROM user WHERE userid='$previousSql';
1 eric
1 eric
2 john
3 carl
What I'd like instead is:
1 eric
2 john
3 carl
I'm making an add members function, and I'd very much not like everyone to show up more than once.. Any directions, help or guiding would be much appreciated.
add
GROUP BY name
to the query
This should do it:
SELECT u.userid, u.name FROM Usertable u
INNER JOIN UserTeam ut ON ut.userid = u.userid
INNER JOIN Teamtable tt ON tt.teamid = ut.teamid
WHERE tt.teamid != 3
GROUP BY u.userid, u.name;
SELECT userid, name FROM usertable
JOIN userteam ON userteam.userid = usertable.userid
JOIN teamtable on teamtable.teamid = userteam.teamid
WHERE teamtable.teamid = 3;
The SQL above should do the proper joins between all the tables and then will only list the userid/names of people who have a teamid of 3 in the teamtable
EDIT* Removed ! from WHERE clause.
I have 3 tables in my database, in which team1 and team2 ids in Matches Table are equal to team_id in TeamNames Table.
also group in Matches Table is equal to group_id in GroupNames Table
Matches Table
-----------------------------------
| team1 | team2 | group | count |
| 3 | 5 | 1 | 1 |
| 1 | 2 | 3 | 0 |
-----------------------------------
GroupNames Table
-----------------------
| group_id | name |
| 1 | Finals |
| 3 | Semi-Final |
-----------------------
TeamName Table
-----------------------
| team_id | name |
| 5 | Flowers |
| 2 | Rainbow |
-----------------------
What I need to get is:
SELECT team1 , team1_name , team2 , team2_name , group , group_name WHERE count=1
I tried joining tables, but as each of the team1 and team2 should be related to unique id in TeamName Table I failed, getting group name was easy, but I failed getting all the above in single query
Questions:
Is this possible in single query?
Can this be done using CodeIgniter's "Active Record Class"?
Answer for q1:yes
Answer for q2:yes
Hope this may help you
$this->db->from('Matches m');
$this->db->select('m.team1,m.team2,m.group,m.count,tn1.name team1_name,tn2.name team2_name,gn.name group_name');
$this->db->join('TeamName tn1','tn1.team_id = m.team1');
$this->db->join('TeamName tn2','tn2.team_id = m.team2');
$this->db->join('GroupNames gn','gn.group_id = m.group');
$this->db->where('m.count',1);
$results=$this->db->get()->result();
yes. with INNER JOIN.
Here is an example to combine your three tables, if you want you can combine more.
SELECT * FROM matches ma INNER JOIN groups gr ON ma.groupid = gr.groupid INNER JOIN teams te ON ma.teamid = te.teamid WHERE ma.count = 1
Good luck!
Martin
I have question about mysql queries. The story goes something like this: I have table in which I store information about college trips. This table has attributes about name of a trip, id of a trip and activity. Activity can be 0 or 1, depending if trip is still active (1) or inactive (0). In second table I have information about students that have applied for trips with attributes: id, name, surname and id of a trip that student have applied for. I don't know mysql query that will show me only students that have applied for trips that are still active (acitivity=1).
For example let's have a look at these tables:
TRIPS
id | trip | activity
---+----------+-----------
1 | Paris | 0
2 | London | 1
3 | Belgrade | 0
4 | Prague | 1
STUDENTS
id | name | id_trip
---+----------+-----------
1 | Mark | 3
2 | Ana | 1
3 | Tom | 2
4 | Maya | 3
5 | Rachel | 4
6 | John | 2
RESULT
id | name | id_trip | trip | activity
---+----------+---------+---------+---------
3 | Tom | 2 | London | 1
5 | Rachel | 4 | Prague | 1
6 | John | 2 | London | 1
SELECT
s.id,
s.name,
s.id_trip,
t.trip,
t.activity
FROM
STUDENTS AS s
INNER JOIN TRIPS AS t ON ( t.id = s.id_trip )
WHERE
t.id = 1
hope this will work.
Try this:
SELECT s.id, s.name, s.id_trip, t.name, t.activity
FROM students s
JOIN trips t
ON s.id_trip = t.id
WHERE t.activity = 1
select * from students s
join trips t
on s.id_trip = t.id
where t. activity =1
Try this it may solve your problem:
select s.id as id, s.name as name,t.trip as trip,
t.activity as activity from trips t
join students s on
t.id = s.id_trip
where t.activity = 1
I have two mysql tables which I've simplified below. I would like to create a query which would pull the data from both tables and order by the most recent date. So if there's an entry (or entries) in the notes table it would look for the most recent notes_date for that cid, and if there's no entry it would use the contact_date for that cid.
contacts
+-----+--------+---------------------+
| cid | name | contact_date |
+-----+------------------------------+
| 1 | george | 2014-03-03 12:24:48 |
| 2 | john | 2014-02-28 15:39:20 |
| 3 | paul | 2014-02-14 10:13:58 |
| 4 | ringo | 2014-02-06 07:13:17 |
+-----+--------+---------------------+
notes
+-----+-----+---------------------+
| nid | cid | notes_date |
+-----+---------------------------+
| 1 | 1 | 2014-03-06 15:43:55 |
| 2 | 1 | 2014-03-14 20:14:12 |
| 3 | 4 | 2014-03-20 22:10:14 |
+-----+-----+---------------------+
This is the result I'd like to get from the query
4 ringo 2014-03-20 22:10:14
1 george 2014-03-14 20:14:12
2 john 2014-02-28 15:39:20
3 paul 2014-02-14 10:13:58
Any help would be much appreciated
This has a few parts to it. One is getting the most recent date for notes. Another is combining this with the contacts data and then choosing the right date.
The following approach uses an aggregation subquery and join to do the calculation:
select c.cid, c.name, coalesce(n.notes_date, c.contact_date) as thedate
from contacts c left outer join
(select n.cid, max(notes_date) as notes_date
from notes
group by n.cid
) n
on c.cid = n.cid
You should use join. You can have query like-
select cont.cid, cont.name, nots.notes_date from contacts cont inner join notes nots on cont.cid=nots.cid order by nots.notes_date
I will try to be as explanatory as possible regarding my question. I am using MYSQL/PHP to fetch data from two tables with the structure looking like the following:
table A
+---------+------------+
| userid | username |
+---------+------------+
| 1 | john |
| 2 | doe |
| 3 | lewis |
+---------+------------+
table B
+---------+------------+-----------+
| id |from_userid | to_userid |
+---------+------------+-----------+
| 1 | 1 | 3 |
| 2 | 3 | 2 |
| 3 | 1 | 2 |
| 4 | 2 | 1 |
+---------+------------+-----------+
Am trying to achieve the following:
+---------+------------+----------------------+
| id |sender username| receiver username |
+---------+------------+----------------------+
| 1 | john | lewis |
| 2 | lewis | doe |
| 3 | john | doe |
| 4 | doe | john |
+---------+------------+----------------------+
As you can see, instead of returning the sender or receiver user id, I am returning their username according to Table A.
can I use left or right joins in this scenario? Thanks in advance
Try this
SELECT b.id, sender.username AS sender_username, receiver.username AS receiver_username
FROM tableB AS b
JOIN tableA AS sender ON b.from_userid = sender.userid
JOIN tableA AS receiver ON b.to_userid = receiver.userid
Inner joins would work fine, you just need to do two of them...
Left and right joins are only needed when you want to get all records from one table and some from another table. In this case you have two distinct relationships thus the need for two joins. My MySQL skills are a bit rusty so I don't know if ' or [ are used as separators on the table/field names with spaces.
Select t1.ID, T1.userName as 'Sender userName', T2.username as 'Receiver username'
FROM [Table A] A
INNER JOIN [Table B] T1
on T1.from_userid = A.userID
INNER JOIN [Table B] T2
on T2.to_userid = A.userID