Not unique table/alias: - mysql

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

Related

no code error in my mysql query yet no results

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)

How to create a nested query with 3 tables in mysql

I have 3 tables, as shown in image.
I want a query which give me all fields from purchase_master, and package_title from package_master (purchase_master.package_id == package_master.id) + user_name from user_master (purchase_master.user_id == user_master.user_id).
I could not figure out how to join 3 tables.
Need some help.
The general idea would be:
SELECT pum.*, pam.package_title, um.user_name
FROM purchase_master AS pum
LEFT JOIN package_master AS pam ON pam.package_id = pam.id
LEFT JOIN user_master AS um ON um.user_id = pum.user_id
WHERE //some condition?
Is this what you require?
Select a.package_id,a.user_id,a.package_details,b.package_title,c.user_name from purchase_master as a join paclage_master as b join user_master as c where a.package_id=b.id and a.user_id=c.user_id
Try this
Joining three tables is same as joining two tables.
You can use following query to achieve this.
SELECT P.PACKAGE_ID,P.USER_ID,P.PACKAGE_DETAILS,PM.PACKAGE_TITLE
FROM PURCHASE_MASTER P
JOIN PACKAGE_MASTER PM ON P.PACKAGE_ID= PM.ID
JOIN USER_MASTER U ON P.USER_ID=U.USER_ID

Left Join table error missing aliases

I wrote a query in mysql to get all records from job_input table and corresponding output records if avaible from job_output table. It gives the following error:
Query: select ji.* from job_input as ji left join (select
SUM(jo.O_Total) AS Total_Output, SUM(jo.O_XS) AS XS_Output,
SUM(jo.O_S) AS ...
Error Code: 1248 Every derived table must have its own alias
Below is my query. What am I doing wrong??
SELECT ji.*
FROM `job_input` AS ji LEFT JOIN
(SELECT
SUM(jo.O_Total) AS Total_Output,
SUM(jo.O_XS) AS XS_Output,
SUM(jo.O_S) AS S_Output,
SUM(jo.O_M) AS M_Output,
SUM(jo.O_L) AS L_Output,
SUM(jo.O_XL) AS XL_Output,
SUM(jo.O_XXL) AS XS_Output,
SUM(jo.O_Other) AS Other_Output FROM `job_output` AS jo GROUP BY jo.`Job_InputID`)
ON jo.`Job_InputID`= ji.`Job_InputID`
You need to put alias to join table?
SELECT ji.*
FROM `job_input` AS ji LEFT JOIN
(SELECT
jo.`Job_InputID` AS JobID
SUM(jo.O_Total) AS Total_Output,
SUM(jo.O_XS) AS XS_Output,
SUM(jo.O_S) AS S_Output,
SUM(jo.O_M) AS M_Output,
SUM(jo.O_L) AS L_Output,
SUM(jo.O_XL) AS XL_Output,
SUM(jo.O_XXL) AS XS_Output,
SUM(jo.O_Other) AS Other_Output FROM `job_output` AS jo GROUP BY jo.`Job_InputID`) AS table2
ON table2.JobID = ji.`Job_InputID`

MySQL JOIN Not unique table/alias:

SELECT punkty_skupu.id, punkty_skupu.nazwa_punktu, punkty_skupu.miejscowosc, trasy.czas_przejazdu_tekst
FROM punkty_skupu LEFT JOIN
punkty_skupu
ON trasy.id_punktu = punkty_skupu.id
Presumably, you are getting an error message because you have an undefined table alias:
SELECT punkty_skupu.id, punkty_skupu.nazwa_punktu, punkty_skupu.miejscowosc,
trasy.czas_przejazdu_tekst
FROM punkty_skupu LEFT JOIN
punkty_skupu trasy
------------------^
ON trasy.id_punktu = punkty_skupu.id

How to get source and destination city name using inner query in mysql

table_cities - city_id, city_name
table_booking - booking_id, source_city_id, destination_city_id
I want to get Booking_id | source_city_name | destination_city_name as result.
I am trying this:
SELECT * FROM table_bookings
INNER JOIN table_cities
ON table_bookings.source_city_id = table_cities.city_id
INNER JOIN table_cities
ON table_bookings.destination_city_id = table_cities.city_id;
But its giving Not unique table/alias: 'table_cities'
can someone help?
You need to add aliases to your query, this is because you join on the table table_bookings twice and select * the table names are ambiguous, so you need to add aliases after your joins to make it clear:
SELECT
table_bookings.Booking_id,
sourceCities.source_city_name,
destinationCities.destination_city_name
FROM table_bookings
INNER JOIN table_cities AS sourceCities
ON table_bookings.source_city_id = table_cities.city_id
INNER JOIN table_cities AS destinationCities
ON table_bookings.destination_city_id = table_cities.city_id;