no code error in my mysql query yet no results - mysql

CREATE TABLE wreport13
AS
SELECT customers.CName, customers.CIDCard, customers.Cphone, customers.infection, users.UName, authlist.AuthType, checkup.comment
FROM ((((checkup
INNER JOIN customers ON customers.CID=checkup.CID)
INNER JOIN users ON users.UID=checkup.EID)
INNER JOIN uauth ON users.UID=uauth.UID)
INNER JOIN authlist ON authlist.AID=uauth.AuthID)
WHERE (checkup.Result=1)
and (checkup.QID=9)
and (checkup.cdate='2020-05-23')
and (uauth.astatus=1)
this query should result 4 records but nothing comes out.
please help me to find the error,
when i used this below query
SELECT cdate,COUNT(CID) AS customers
FROM (checkup
INNER JOIN questionaire ON questionaire.QID=checkup.QID)
WHERE checkup.Result=1 and checkup.QID=9
GROUP BY cdate
and result is
cdate customers
2020-05-23 4
2020-05-25 1
2020-05-30 3
2020-05-31 2
the first query is meant to extract the details of the first row of the above query

Sorry, it was a mistake in inner join key with customers table
SELECT customers.CName, customers.CIDCard, customers.Cphone, customers.infection,
users.UName, authlist.AuthType, checkup.comment
FROM ((((checkup
INNER JOIN customers ON customers.CID=checkup.CuID)
INNER JOIN users ON users.UID=checkup.EID)
INNER JOIN uauth ON users.UID=uauth.UID)
INNER JOIN authlist ON authlist.AID=uauth.AuthID)
WHERE (checkup.Result=1)
and (checkup.QID=9)
and (checkup.cdate='2020-05-23')
and (uauth.astatus=1)

Related

How do I perform this join query?

I have a table leave_form which looks like:
type id reporting_id leave_bal from_date_id leave_from to_date_id leave_to number leave_for status applied_dates_id pendays
personal 99 6 10 1023 full day 1313 full day 10 personal yes 1026 null
I have separate table for dates, so that I can refer these dates into leave_form. My leave_date table looks like:
date_id(AI) dates(UK)
1025 2016-02-18
1301 2016-02-20
1218 2016-02-16
This date_id I have inserted into from_date_id, to_date_id, applied_dates_id columns in leave_form table i.e. all dates are inserted into leave_date table and from this table I am only referring the date_id into leave_form table.
There is also a table that keeps the emp_code and emp_name. My personal table is:
id(AI) emp_code(PK) emp_name
99 K0209 Nijo
When I am trying to fetch the date for from_date_id, to_date_id, applied_dates_id column from leave_form table I don't get any values.
My query for fetching the dates is:
select g.type, a.emp_code, h.rm_id, h.rm_name, g.leave_bal, i1.dates as from_date,
g.leave_from, i2.dates as to_date, g.leave_to, g.number, g.leave_for, g.status,
i3.dates as applied_date, g.pendays
from personal a
inner join leave_form g
on a.id = g.id
inner join inform_to_rm h
on h.reporting_id = g.reporting_id
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
where a.emp_code = 'K0209';
It shows me result like:
type, emp_code, rm_id, rm_name, leave_bal, from_date, leave_from, to_date, leave_to, number, leave_for, status, applied_date, pendays
i.e no data gets returned when I am executing this query.
I would agree with one of the comments to the question. I would recommend referencing the date directly in the leave_form table instead of a FK to a table with dates. But back to the question. You haven't described all of your tables completely, so it is possible that there are multiple problems that I can't see, however, there is definitely one problem.
Your query joins on
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
This is incorrect. leave_dates.dates is the actual DATE, while the columns that you are joining on (leave_form.from_date_id, leave_form.to_date_id, leave_form.applied_dates_id) are foreign key references.
For example, 1023 does not equal 2016-02-18 so you get no match. Replacing the above query-snippet with the following would correct this particular problem.
inner join leave_dates i1
on i1.date_id = g.from_date_id
inner join leave_dates i2
on i2.date_id = g.to_date_id
inner join leave_dates i3
on i3.date_id = g.applied_dates_id

Mysql query with multiple joins and conditions

I am trying to connect four tables using joints,here i used left join to connect tables and my condition is all the goods,item should be same and all the site should be same.same site have multiple goods, so i want to get the sum number of goods from each table. my query is given
select
a.goods
,sum(a.no_of_units) as totala
, a.site
,b.item
,sum(b.quantity) as totalb
,b.site
,c.goods
,c.site
,sum(c.no_of_units) as totalc
,d.site
,d.goods
,sum(d.quantity)b as totald
from
inward_stock a
left join
opening_balance b
on
a.site=b.site
and
a.goods=b.item
left join
return_stock c
on
b.site=c.site
and
b.item=c.goods
left join
stock_consumed d
on
d.site=c.site
and
d.goods=c.goods
Can you put your conditions at the end of the joints like this:
select a.goods,sum(a.no_of_units) as totala, a.site,b.item,sum(b.quantity) as totalb,b.site,c.goods,c.site,sum(c.no_of_units) as totalc,d.site,d.goods,sum(d.quantity) as totald
from inward_stock a left join
opening_balance b on (a.site=b.site) left join
return_stock c on (b.site=c.site) left join
stock_consumed d on (d.site=c.site) where (a.goods=b.item) and (b.item=c.goods) and (d.goods=c.goods)
I have not tested your request but it seems that it's not bad.

Not unique table/alias:

Here's a query, i'm getting this error #1066 - Not unique table/alias: 'tbl_cp_list'
I have 2 database ie.
1) grameenphone_bill ---> 1 table ---> tbl_admin
2) android_appstore ---> 2 Tables ---> tbl_cp_list, tbl_list_data
SELECT `grameenphone_bill`.`tbl_admin`.`cp_id`,`grameenphone_bill`.`tbl_admin`.`cp_name`,`android_appstore`.`tbl_cp_list`.`cpid`,`android_appstore`.`tbl_cp_list`.`cpname`,`android_appstore`.`tbl_list_data`.`cp`,`android_appstore`.`tbl_list_data`.`Count`
FROM
`android_appstore`.`tbl_cp_list`
INNER JOIN `grameenphone_bill`.`tbl_admin`
ON `grameenphone_bill`.`tbl_admin`.`cp_id`=`android_appstore`.`tbl_cp_list`.`cpid`
INNER JOIN `android_appstore`.`tbl_cp_list`
ON `android_appstore`.`tbl_cp_list`.`cpname`=`android_appstore`.`tbl_list_data`.`cp`
Please tell , where i'm going wrong?
Thanks!!
You had return wrong table name in last JOIN.
Try this:
SELECT grameenphone_bill.tbl_admin.cp_id,grameenphone_bill.tbl_admin.cp_name,android_appstore.tbl_cp_list.cpid,
android_appstore.tbl_cp_list.cpname,android_appstore.tbl_list_data.cp,android_appstore.tbl_list_data.Count
FROM android_appstore.tbl_cp_list
INNER JOIN grameenphone_bill.tbl_admin ON grameenphone_bill.tbl_admin.cp_id=android_appstore.tbl_cp_list.cpid
INNER JOIN android_appstore.tbl_list_data ON android_appstore.tbl_cp_list.cpname=android_appstore.tbl_list_data.cp
You can also use alias names for tables
SELECT b.cp_id, b.cp_name, a.cpid, a.cpname, c.cp, c.Count
FROM android_appstore.tbl_cp_list a
INNER JOIN grameenphone_bill.tbl_admin b ON b.cp_id = a.cpid
INNER JOIN android_appstore.tbl_list_data c ON a.cpname = c.cp

MySQL Complex Inner Join Query getting error

Query Definition:
Select Students who have a grade of 85 or better in art
and
who also have a grade of 85 or better in any computer course
QUERY(explained in 3 sections is single query executed on MySQLWorkBench):
select Students.StudFirstName,Student_Schedules.Grade
from
(Select Distinct Students.StudentID,Students.StudFirstName,Student_Schedules.Grade
from (((Students
Inner Join Student_Schedules
On Student_Schedules.StudentID = Students.StudentID)
Inner Join Classes
On Classes.ClassID = Student_Schedules.ClassID)
Inner Join Subjects
On Subjects.SubjectID = Classes.SubjectID)
Inner Join Categories
On Subjects.CategoryID = Categories.CategoryID
where Categories.CategoryDescription = 'Art' and Student_Schedules.Grade >= 85)
As Stud_Art
Above code extracts Students who have a grade of 85 or better in Art
Inner Join
(Select Distinct Students.StudentID,Students.StudFirstName,Student_Schedules.Grade
from (((Students
Inner Join Student_Schedules
On Student_Schedules.StudentID = Students.StudentID)
Inner Join Classes
On Classes.ClassID = Student_Schedules.ClassID)
Inner Join Subjects
On Subjects.SubjectID = Classes.SubjectID)
Inner Join Categories
On Subjects.CategoryID = Categories.CategoryID
WHERE Categories.CategoryDescription LIKE '%Computer%' AND Student_Schedules.Grade >= 85)
As Stud_CS
Above code extracts Students who have a grade of 85 or better in Computer
On Stud_CS.StudentID = Stud_Art.StudentID;
Above code matches StudentID from Art and Computer
Error From MySQLWorkBench:
Error Code: 1054. Unknown column 'Students.StudFirstName' in 'field list'
The above Query may be solved by SubQuery technique but i want to learn how is it possible with Inner Join technique
You are trying to retrieve Students.StudFirstName and Student_Schedules.Grade but your table aliases are Stud_Art and Stud_CS. Might that be the issue?
I think that the first part should be like this:
select Stud_Art.StudFirstName,Stud_Art.Grade

mysql left join with multiple tables,having only primary key as foreign key of first table

TB 1 : user_profile as ur-> id(PK),name (total 4k records all unique)
TB 2 : user_course_rel as ucr-> id,course_id,year_id,division,user_id(Fk)
TB 3 : students_attendance_lect as sal-> id,subject_id,date,student_id(Fk)
student_id(Fk) = user_id(Fk) = id(PK).
I want to left join on TB1 and get name of all students belonging to particular course,year,division and both attendees of subject and date and not attendees which should be 132 unique records.
After running following query i am getting total (4k records)
select distinct(ur.id), ur.fname
from user_profile as ur
inner join user_course_rel as ucr
on ucr.user_id=ur.id
left join students_attendance_lect as sal
on sal.student_id=ucr.user_id
and ucr.course_id=1
and ucr.year_id=1
and ucr.division=3
and sal.subject_id=2
and sal.date='2013-01-21'
Several items in your LEFT JOIN look like they should be in a WHERE clause. I'm not 100% clear what your question is but try:
select distinct(ur.id), ur.fname
from user_profile as ur
inner join user_course_rel as ucr
on ucr.user_id=ur.id
left join
(SELECT sal.student_id, sal.subject_id, sal.date
FROM students_attendance_lect as sal
WHERE sal.date='2013-01-21'
AND sal.subject_id = 2) AS sa
ON sa.student_id=ucr.user_id
WHERE ucr.course_id=1
and ucr.year_id=1
and ucr.division=3
The way you had written it was asking the DB to LEFT JOIN on any row that had a course id of 1, a division of 3, a subject id of 2 or a date of '2013-01-21', do you see?