In MySql database have AM_COURSE table in that
cId CourseName course_desc
101 java sometext...
102 mysql sometext....
:
AM_USER in that
uid name
1001 Ram
1002 pavan
AM_INTER in that
id uid cid
1 1001 101
2 1001 102
3 1002 101
AM_TIMETABLE table in that
UserId Date_Time
101 2012-08-08 04:00:00
102 2012-08-15 10:00:00
103 2012-08-18 09:00:00
104 2012-08-24 05:00:00
My Question is, i'm passing start-date and end-date and CourseName then i want to get details like userId,name,coursename,course_desc in that way i wrote query
SELECT a.cid,a.course_desc,c.name
FROM
AM_COURSE a,AM_USER c INNER JOIN AM_TIMETABLE b ON a.S_ID = b.COURSE_ID WHERE
a.Name = 'java'
AND
b.Date_Time BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00'
but i'm getting error please help
The INNER JOIN keyword returns rows when there is at least one match in both tables.
MySQL INNER JOIN clause
The MySQL INNER JOIN clause matches rows in one table with rows in other tables and allows you to query rows that that contain columns from both tables.
The MySQL INNER JOIN clause an optional part of the SELECT statement. It appears immediately after the FROM clause.
Before using MySQL INNER JOIN clause, you have to specify the following criteria:
First, you have to specify the main table that appears in the FROM clause.
Second, you need to specify the tables that you want to join with the main table, which appear in the INNER JOIN clause. Theoretically, you can join a table with many tables. However, for a better query performance, you should limit the number of tables to join.
Third, you need to specify the join condition or join predicate. The join condition appears after the keyword ON of the INNER JOIN clause. The join condition is the rule for matching rows between the main table and the other tables.
Example
SELECT c.cid, c.course_desc, user.name FROM AM_COURSE c
INNER JOIN AM_INTER inter on inter.cid = c.cid
INNER JOIN AM_USER user on user.uid = inter.uid
INNER JOIN AM_TIMETABLE tt ON inter.cid = tt.UserId
WHERE c.Name = 'coursename' AND tt.Date_Time BETWEEN '2011-08-12' AND '2012-08-12'
try
SELECT course.cid, course.course_desc, user.name
FROM AM_COURSE course
INNER JOIN AM_INTER inter on inter.cid = course.cid
INNER JOIN AM_USER user on user.uid = inter.uid
INNER JOIN AM_TIMETABLE tt ON inter.cid = tt.UserId
WHERE course.Name = 'java'
AND tt.Date_Time BETWEEN '2012-08-07' AND '2012-08-20'
Related
I have three tables tblCourse , tblDegree , tblStudent. I have created a course and degree relation table as like tblCourseDegreeRelation. This relational table uses foreign keys of course and degree table as like:
The tblCourseDegreeRelation table is like:
The tblCourse table is like:
The tblDegree table is like:
The tblStudent (In this table the degree id is foreign key d_id) table is like:
I need to get all records including the tblStudent all record using this query:
SELECT * from tbldegree d
INNER JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
INNER JOIN tblcourse c ON cdr.c_id = c.c_id
INNER JOIN tblstudent s ON d.d_id = s.d_id
ORDER BY cdr.cdr_id DESC
But this only returns the one student's record while I've two students in the database see below:
How I can get all students records from the joins query?
In your case you have all inner joins, so it will return results where both/all tables satisfies their criteria (on clause).
Viewing your data your student 1 => Ali has a relation with degree 1 =>BS Information Technology.
Further degree 1 has courses (1 => Programming, 2 => English, 5=> Mathematics , 6 => Electronics)
So for student 1 your inner join clause works because it has data in all joined tables.
Now if we look for your student 3 => Bilal who has a relation with degree 3 => BS Mathematics, But this degree has no assigned courses that is why your student Bilal isn't returned
To get all students no matter their related degree has courses you can turn your inner joins into left join not for all tables but for tblcoursedegreerelation and tblcourse
SELECT *
FROM tblstudent s
INNER JOIN tbldegree d ON d.d_id = s.d_id
LEFT JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
LEFT JOIN tblcourse c ON cdr.c_id = c.c_id
ORDER BY cdr.cdr_id DESC
Demo
In the result set you can see following columns as null due to no association with courses
cdr_id, c_id, d_id, c_id, c_name, c_credit
Just do a Right join on tblstudent:
SELECT * from tbldegree d
INNER JOIN tblcoursedegreerelation cdr ON d.d_id = cdr.d_id
INNER JOIN tblcourse c ON cdr.c_id = c.c_id
RIGHT JOIN tblstudent s ON d.d_id = s.d_id
ORDER BY cdr.cdr_id DESC
EDIT
This way is better:
SELECT c.d_id,c.d_name,c.d_fee,cdr.cdr_id,cdr_c_id,deg.c_name,deg.d_credit,a.s_id,a.s_name
FROM tblstudent a
left join tblDegree c ON a.d_id = c.d_id
left join tblcoursedegreerelation cdr ON cdr.d_id=c.d_id
left join tblcourse deg on deg.c_id=cdr.c_id
ORDER BY cdr.cdr_id DESC
with below Sql query i can get result successful when all of tables has data, but in this my query when transactions table has not any saved data and its empty my query return empty result, but i want to get null or empty columns data
SELECT transactions.id,
userEwallets.ewalletNumber,
userEwallets.currencySymbol,
transactions.money,
transactions.transactionType,
b.username AS toUser,
a.username AS sender
FROM transactions
JOIN userEwallets ON transactions.ewalletId = userEwallets.id
LEFT JOIN users AS b ON b.id = transactions.toUserId
LEFT JOIN users AS a ON a.id = transactions.fromUserId
WHERE transactions.userId = 37
when its not empty i get this result:
id ewalletNumber currencySymbol money transactionType toUser sender
95 SHIRR9373036569 IRR 20 1 1 amin
you can use a dummy table with one row. The other tables should be left joined to it.
SELECT transactions.id,
userEwallets.ewalletNumber,
userEwallets.currencySymbol,
transactions.money,
transactions.transactionType,
b.username AS toUser,
a.username AS sender
FROM (select 1) dummy
LEFT JOIN transactions ON transactions.userId = 37
LEFT JOIN userEwallets ON transactions.ewalletId = userEwallets.id
LEFT JOIN users AS b ON b.id = transactions.toUserId
LEFT JOIN users AS a ON a.id = transactions.fromUserId
You can allways use a subquery instead of a table name if you give it an allias. Note that you have to move the WHERE condition for the left joined table into the ON clause - Othewise MySQL will convert it to an INNER JOIN.
You are using where condition here. where transactions.userId = 37. If there is no data in transactions table, that where condition will fail and hence returns no data
In order to accomplish that, you should use another table to be the first one. The way you are doing it, if there are no records in the transactions table then there would be nothing to join with.
So, use a table that always has records and LEFT JOIN it with the others.
Try this:
SELECT transactions.id,
userEwallets.ewalletNumber,
userEwallets.currencySymbol,
transactions.money,
transactions.transactionType,
b.username AS toUser,
a.username AS sender
FROM users AS b
LEFT JOIN transactions ON b.id = transactions.toUserId
LEFT JOIN users AS a ON a.id = transactions.fromUserId
JOIN userEwallets ON transactions.ewalletId = userEwallets.id
WHERE transactions.userId = 37
SELECT users.id as uid, projects.id as pid
FROM users
inner join usergroup on usergroup.id = users.user_group
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE bookings.status = 'Accepted' AND units.status = 'Sold'
GROUP BY pid
with the query above, i get the correct and logic output:
pid | uid
1 1
2 1
9 12
10 14
then i want to show the user's name, so i added 1 field in query as shown below:
SELECT users.id as uid, users.f_name, projects.id as pid
FROM users
inner join usergroup on usergroup.id = users.user_group
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE bookings.status = 'Accepted' AND units.status = 'Sold'
GROUP BY pid
but i got a different output for uid:
pid | uid
1 1
2 1
9 9
10 11
This is not logic and anyone know why? Let's assume both are correct but why will output different result?
Your 2nd query you are filtering data with join condition before where condition while in 1st query you are filtering data just in where clause...keep you 2nd query same as 1st to get same results....
try below query-
SELECT projects.id as pid, users.id as uid, users.f_name
FROM users
inner join usergroup on usergroup.id = users.user_group
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE bookings.status = 'Accepted' AND units.status = 'Sold'
GROUP BY pid
One project ID can be associated to many units and each unit with many bookings and hence with many users. You group by project ID, but you don't specify which of all associated users you want to see for a project ID. You would do this with an aggregate function such as MIN(users.id), MAX(users.id), etc. But you don't use such function, thus telling MySQL: "just give me randomly one of the matching users".
As long as you keep your query as is, it seems that MySQL always gives you the same users, maybe the first it finds. This is by no way guaranteed; you could just as well get different users with the same query.
Now that you changed your query, MySQL goes another route and picks different matching users.
i want to join 2 mysql tables .but join information is in a separate table .let's say i have 3 table named student ,course and reg contain id of student and course he does.
student table
s_id | name
1 | miki
2 | foly
3 | oski
course table
c_id | name
101 | c++
102 | java
103 | ruby
reg table
s_id | c_id
1 | 101
1 | 102
2 | 101
now i want to get all the course someone do.i wrote sql query for that without using join query .but i want to do same thing using join query .this is my query
SELECT c.name FROM student as s,course as c,reg as r where r.s_id=s.s_id and r.c_id=c.c_id and s.name='miki';
Statement
SELECT c.name
FROM student as s,couse as c,reg as r
where r.s_id=s.s_id and r.c_id=c.c_id and s.name='miki'
is join too, , between table names is short cut for cross join, so you already using joins (actually you have some conditions in where, so RDBMS will optimize it to inner join)
but, of course you can rewrite it to different syntax:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id and r.s_id=(select s_id from student where name='miki'));
another syntax:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id)
inner join
student as s
on (r.s_id=s.s_id and s.name='miki');
and another one:
SELECT c.name
FROM couse as c
inner join
reg as r
on (c.c_id = r.c_id)
inner join
student as s
on (r.s_id=s.s_id)
where s.name='miki';
depending on bunch of conditions performance of these 4 queries can be different, but results will be the same
Just join all 3 tables to get the result
select c.name
from course c
join reg r on r.c_id = c.c_id
join student s on s.s_id = r.s_id
where s.name = 'miki'
SELECT s.name,
c.name
FROM student s
JOIN reg r
ON r.s_id = s.s_id
JOIN course c
ON c.c_id = r.c_id
WHERE s.name = "miki"
SELECT c.name, s.name as stud FROM course c
JOIN reg r on c.c_id = r.c_id
JOIN student s on r.s_id = s.s_id
WHERE s.name = 'miki'
I have a main table:
- cam (id, name, type, desc, tenant_id)
with information normalized into 3 different tables:
- cs with columns (cam_id, st_id)
- ot with columns (cam_id, loc_id, tar_id, tenant_id)
- st with columns (id, code, name)
please note:
- cs.st_id is a foreign key to st.id
- ot.loc_id is a foreign key to st.id,
- ot.tar_id is a foreign key to st.id
My intention is to get the
- st.code value for all ot.loc_id and cs.st_id
(I am not interested in the id's but the their codes which is stored in table st)
This SQL:
- select
cam.id, cam.name, camp.type, cam.desc, st.code as cs.code
from
cam
left join cs on cam.id = cs.cam_id
left join ot on cam.id = ot.cam_id
left join st on cam.tenant_id = st.tenant_id;
works in that the last join condition to st table makes the st.codes available.
But what do I have to do to get the ot.loc_id codes?? I can't have multiple from clauses right? or multiple tables in from clause ... right?
Or is there no way out but to make separate SQL statements (which may not be performant i.e making an additional call)?
Thanks!
Take-Away: The join condition does not need to include the table in the from clause! Please see answer below.
No you can't have multiple from clauses
Yes you can have multiple tables in from clause
For example you could do :
select a.id, a.name, a.type, a.desc, b.code as cs.code, b.code
from cam a, st b, cs c, ot d
where a.id = c.cam_id
and a.id = d.cam_id
and st.id = d.loc_id
and b.tenant_id = a.tenant_id
Or your could simply refer to field in joined tables or views in the select statement.
When you do a join, you "join" the table to your select statement and can access them.
Example with join :
select cam.id, cam.name, camp.type, cam.desc, st.code as cs.code, st.code
from cam
left join cs on cam.id = cs.cam_id
left join ot on cam.id = ot.cam_id
left join st on cam.tenant_id = ot.tenant_id
left join st st2 on st.id = ot.loc_id;
Is your intention to join to the st table twice?
select cam.id, cam.name, camp.type, cam.desc, st.code as cs_code , st.code as loc_code
from cam left join
cs
on cam.id = cs.cam_id left join
ot
on cam.id = ot.cam_id left join
st
on cam.tenant_id = st.id left join
st stloc
on ot.loc_id = stloc.id;
According to the columns in your tables, st doesn't have a tenant_id so I changed that to just id.