Query Mysql Error : Not unique table/alias - mysql

Can you please help me out. I have this SQL query:
select tenagapengajars.id, tenagapengajars.nama, tenagapengajars.pendidikan, tenagapengajars.created_at, programstudis.nama
from tenagapengajars
LEFT JOIN
tenagapengajars
ON programstudis.id = tenagapengajars.id_prodi
And somehow it says
#1066 - Not unique table/alias: 'tenagapengajars'

You are selecting tenagapengajars and also joining on tenagapengajars. If you want to do that, you have to specify an alias. Otherwise MySQL doesn't know chich table you mean.
from tenagapengajars t1
LEFT JOIN
tenagapengajars t2
You are also selecting data from programstudis, which is not in the query. You probably just copied the wrong table and meant something like:
select tenagapengajars.id, tenagapengajars.nama, tenagapengajars.pendidikan, tenagapengajars.created_at, programstudis.nama
from tenagapengajars
LEFT JOIN
programstudis
ON programstudis.id = tenagapengajars.id_prodi

Related

Unknown column 'r' in field list

I've been working on a SQL query for a project, and I face an error message when I want to use it.
Here is the query itself :
SELECT COUNT(r) AS auditMade,
SUM(g.nbrMilkingCows) AS cowsAudited,
AVG(r.gainPerCowPerYearTransition) AS averageGainTransition,
AVG(r.gainPerCowPerYearLactation) AS averageGainLactation,
AVG(r.totalGain) AS averageTotalGain,
AVG(r.supplementalCostPerCow) AS averageSuppCost
FROM `smart_calculator_infos` i
INNER JOIN `smart_calculator_result` r ON r.idSmartCalculatorResult = i.idSmartCalculatorResult
INNER JOIN `calculator_general_informations` g ON g.idSmartCalculatorInfo = i.idSmartCalculatorInfo
WHERE i.idUser = 14
MySQL answers me "Unknown column 'r' in field list".
But I dont really understand why I get an error here as I define r in my INNER JOIN.
I'm kinda new at using SQL so maybe there is something pretty obvious I forgot, but I can't seem to understand what.
You can't count an alias itself, so the very first line of your query is what is causing the error:
SELECT COUNT(r)
To remedy this, you could use COUNT(*):
SELECT COUNT(*)
Or, you could count an actual column in the smart_calculator_result table, e.g.
SELECT COUNT(r.idSmartCalculatorResult)

Convert SELECT statement to an UPDATE statement in MySQL

I have already read a majority of this similarly asked question but due to the specific nature of most of the answers, none helped, or I was unable to apply the answer.
I need to convert this SELECT statement into an UPDATE statement, the SELECT statement works just fine.
SELECT sf_state_filings_cstm.recurrency_c, sf_state_filings_cstm.date_filed_c,
sf_state_filings_cstm.status_c
FROM sc_state_configuration_cstm
INNER JOIN sc_state_configuration_sf_state_filings_1_c ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
INNER JOIN sf_state_filings_cstm ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
WHERE sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
This is what I have so far after working through the following errors:
When I first wrote the update statement, I got:
MySQL code error 1066: Not unique table/alias: ‘sf_state_filings_cstm
Solution: Why does this SQL code give error 1066 (Not unique table/alias: 'user')?
Then I got this error:
1052: Column ‘recurrency_c' in field list is ambiguous
Solution: 1052: Column 'id' in field list is ambiguous
Now I have this error:
Error : Unknown column 'sc_state_configuration_cstm.id_c' in 'where
clause'
None of the below links have helped so far, or I was doing something wrong
Unknown Column In Where Clause
Unknown Column in Where Clause
MySQL: "Unknown column in where clause" during Update Statement
I have a feeling the answer has to do with using an alias as mentioned in one the links, using HAVING instead of WHERE, but just replacing WHERE with HAVING doesn't seem to work.
Here is my syntax right now:
UPDATE `my_database`.`sf_state_filings_cstm`
LEFT OUTER JOIN sc_state_configuration_sf_state_filings_1_c AS joined_tables ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
LEFT OUTER JOIN sf_state_filings_cstm AS main_table ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
SET main_table.recurrency_c = 'Perpetual',
main_table.expiration_date_c = ''
WHERE
sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
EDIT 1:
Here is how the Tables have to be linked to each other:
I also realized, I need to be doing a LEFT OUTER JOIN instead of an INNER JOIN. I have updated the above syntax. The middle table stores the id's from both tables. That's how the relationship is stored. If more information is needed, let me know.
[SOLUTION]
UPDATE sc_state_configuration_cstm
LEFT OUTER JOIN sc_state_configuration_sf_state_filings_1_c ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
LEFT OUTER JOIN sf_state_filings_cstm ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
SET sf_state_filings_cstm.recurrency_c = 'Perpetual',
sf_state_filings_cstm.expiration_date_c = null
WHERE
sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
Thanks to jyncka and a lot of other posts I read, where i noticed that when converting a SELECT to an UPDATE, they simply wrote UPDATE (table from the 'FROM' statement).
I went back and noticed I had written:
FROM sc_state_configuration_cstm
The error is telling you what's happening: id_c is not a column that exists in the sc_state_configuration_cstm table. If you do a DESCRIBE on sc_state_configuration_cstm it will show you the correct field name and you can drop that in instead of id_c.
Or you might have used the wrong alias in your WHERE statement. Without knowing what your tables look like, it's difficult to say which is the case.
Edit: I believe I see the problem. Here are the tables you are explicitly using in the statement:
sc_state_configuration_sf_state_filings_1_c
sf_state_filings_cstm
But you are using this table in the WHERE clause:
sc_state_configuration_cstm
You need to join sc_state_configuration_cstm so that it can be used. It took me a minute to see it because sc_state_configuration_cstm and sf_state_filings_cstm look similar at first glance.

MySQL Error You can't specify target table for update in FROM clause

I have a 2 tables MST_customer and TRN_sales in my database with corrupt entries. The next query returns the corrupt entries:
SELECT TRN_sales.cust_no
FROM MST_customer
RIGHT OUTER JOIN TRN_sales
ON MST_customer.cust_no = TRN_sales.cust_no
WHERE MST_customer.cust_name IS NULL;
I tried to delete them executing:
DELETE FROM mydbB.TRN_sales
WHERE TRN_sales.cust_no IN (
SELECT TRN_sales.cust_no
FROM MST_customer
RIGHT OUTER JOIN TRN_sales
ON MST_customer.cust_no = TRN_sales.cust_no
WHERE MST_customer.cust_name IS NULL
);
But I get the next error:
You can't specify target table 'TRN_sales' for update in FROM clause
How can I resolve this problem ?
To be a bit more on "the safe side" you should specifiy the table (here: alias name s) you want to delete from like:
DELETE s FROM TRN_sales s
LEFT JOIN MST_customers ON MST.cust_no=TRN.cust_no
WHERE MST.cust_name IS NULL;
Personnaly I believe, this LEFT JOIN is easier to read, although of course you can do the same with your RIGHT JOIN version.
Why you don't try below-
DELETE TRN.*
FROM MST_customer MST
RIGHT OUTER JOIN TRN_sales TRN
ON MST.cust_no = TRN.cust_no
WHERE MST.cust_name IS NULL;
Note: For safe side keep backup of both tables before executing this query.

Joining derived tables

EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;

getting unknown column error even when the column is present in mysql table

select tab1.*(
select a.*
from fw_invi a left join fw_resp b on a.id=b.did,
fw_resp fra left join(
select *
from fw_type
) tab4 on fra.qaild=tab4.qdetailid //this causing error
)tab1 left join jos_users u on tab1.consu=u.id order by tab1.createdon desc
On running the above query in mysql i am getting the following error,which should not be the case as the specified missing column is present in that table.i think i am doing the wrong nested table aliasing.
Unknown column 'tab4.qdetailid' in 'on clause'
1.Why i am getting the error even though the column is present.?
2.Is my above query syntax correct?
Thanks in advance.
* can't be used with aliases. Aliases are used for named columns.