I want to show employee's first name from employee table, but when I execute the query it always returns this error
#1066 - Not unique table/alias
This is my query:
SELECT `employee`.`Fname`
FROM `employee`
LEFT JOIN `company07`.`employee`
ON `employee`.`Ssn` = `employee`.`Super_ssn`
ORDER BY `employee`.`Fname` ASC
Q: Why is MySQL returning error 1066?
Q: How do I change the table alias to avoid error 1066?
There are two table references with the same name "employee". And this isn't valid.
The workaround is to assign an alias to one of the table references. That will make the names unique.
Frequently, we assign a short alias to every table reference. And we use that alias to qualify the column references.
In the following example, we assign the alias e to one of the tables, and the alias c to the other.
SELECT e.`fname`
FROM `employee` e
LEFT
JOIN `company07`.`employee` c
ON c.`ssn` = e.`super_ssn`
ORDER
BY e.`fname` ASC
I just guessed which table "ssn" is from and which table "super_ssn" is from. In the original query, it's ambiguous. Both are qualified with the table name employee. But we don't know which of the tables is being referred to. MySQL has the same problem... which table does "employee" refer to. And this is why MySQL requires that every table name (or alias) needs to be unique.
Related
What the title says. I've tried connecting two tables with two foreign keys referencing the same primary key and cannot get it to work.
Use table aliases. For example:
select *
from money_transfer t
join account s on t.sender_id = s.id
join account r on t.receiver_id = r.id
Here you can see two references to the same PK (account.id). The first time the table is given the alias s and the second time the same table is given the alias r. This way you can differentiate which one you want to use on each join predicate.
So I have the following hierarchical database structure:
Table person has columns id and some other fields.
Table car has columns id, owner (with a foreign key constraint to person.id) and some other field
Table bumpersticker has columns id, car (with a foreign key constraint to car.id) and some other fields
I want to INSERT a row in to bumpersticker and have values to populate the row. I also have a person.id value of the person trying to add the bumpersticker.
What is the best practice to ensure that the car.owner value selected from the bumpersticker.car is in fact the same person.id as I have?
I guess one obvious way is to first execute a select query, on the car table and select the car.owner and validate that this value is the same value as the id of the person trying to add the bumpersticker and then execute an insert query.
but this seems like something there must be an elegant solution to in MySQL. at least not having to do two separate queries.
Most thankful for your help!
You can insert from a SELECT query that tests if the owner matches the criteria
INSERT INTO bumpersticker (car, sticker_text)
SELECT c.id, "If you can read this you're too close"
FROM car AS c
WHERE c.id = #car_id AND c.owner = #person_id
#car_id is the ID of the car you're adding the bumpersticker for, and #person_id is the ID of the user doing the insert. If the owner ID doesn't match, the SELECT query will return no rows, so nothing gets inserted.
DEMO
I have 2 sql separated tables in my database:
ds_users, containing: group_id
and
ds_users_data_members, containing: data_gender
I would like to set / Update the group_id to 6 for all data_gender equal to 2.
All this morning i tried to solve this issue , without success.
Please help. Thank you very much
I am assuming there must be a relation between those two tables. Without any relationship you cannot update record in one table by checking condition in another table.
let's say ds_users table has column user_id which is also exist in ds_users_data_members table.
so, you can write following query to update all records in ds_users for data_gender=2 in ds_users_data_members table
SQL SERVER EXAMPLE
UPDATE T
SET group_id=6
FROM ds_users T INNER JOIN ds_users_data_members T1 ON T.user_id=T1.user_id
WHERE T1.data_gender=2
MySQL EXAMPLE
UPDATE ds_users T INNER JOIN ds_users_data_members T1 ON T.`user_id`=T1.`user_id`
SET T.`group_id`=6
WHERE T1.`data_gender`=2;
You can replace the column name of user_id what you have given in your table.
This should be a pretty simple thing, but I'm quite new to (My)SQL.
Basically, given a customers table with attributes id, client, etc., where the client field is not necessarily unique, I want to eliminate rows where the client field is a duplicate of a previous value.
The following:
SELECT MIN(id) FROM customers GROUP BY client
returns the unique id's of the rows I want. I want everything else out.
I tried
DELETE FROM customers WHERE customer.id NOT IN
(SELECT MAX(id) FROM customers GROUP BY client)
to no avail. (ERROR 1093 (HY000): You can't specify target table 'customers' for update in FROM clause).
Why doesn't it work and what do I need to do to accomplish my goal?
Thank you.
You could create a temporary table that holds the values you want to remove. Then your delete query could be based on that temporary table.
Is there any way to use a create table with select statement where column names in conflict (or all) are aliased?
CREATE TABLE newTable
SELECT a.*, b.*
FROM tblA a
JOIN tblB b
ON a.id = b.cid
The issue is that tblA and tblB have a few columns with the same name, so I get a "duplicate column name" error on the create. I'm trying to avoid listing all the fields in the table, so I either need to selectively exclude some columns or apply and "auto alias" to the column names.
You can use the information_schema table to selectively exclude columns in a select statement. See the top answer here.