1

I have a MySQL problem I can not get to solve. I have a mysql to manage a virtual user dovecot installation, that uses two tables (one for the aliases, another for the domains).

The table aliases has these fields: domain_id(INT), source(VARCHAR), destination(VARCHAR), whereas table domains has only two fields: id (INT AUTO INC) and name (VARCHAR).

Although I'm able to select aliases that belong to a given domain by issuing:

SELECT valias.* FROM aliases AS valias
JOIN domains AS vdomains ON valias.domain_id=vdomains.id 
WHERE vdomains.name = "domain_name";

I can not get to work to insert a new alias, specifing the domain name. something like this:

INSERT INTO valias(domain_id, source, destination) 
    VALUES (id, 'canto', 'george') 
    SELECT id FROM aliases 
    JOIN domains AS vdomains ON aliases.domain_id=vdomains.id 
    WHERE  vdomains.name =     "domain_name";

Does somebody know how to solve this problem?

1
  • If i understood properly then you have used INSERT INTO valias where "valias" is not any table. So I think you can't used that. Insert statement must have either tablename or viewname. Commented Jul 20, 2013 at 16:32

3 Answers 3

2

My experience is mainly in MS SQL Server, but I reckon it should go the same way in MySQL:

INSERT INTO valias(domain_id, source, destination)  
SELECT id, 'canto', 'george' FROM vdomains
WHERE  name = 'domain_name';
Sign up to request clarification or add additional context in comments.

Comments

0

Either I'm missing something here or your query seems a bit to overingeenered. How about this:

INSERT INTO aliases(domain_id, source, destination) 
    VALUES (id, 'canto', 'george')
    JOIN domains ON domains.id = aliases.domain_id
    WHERE domains.name = 'domain name'  

Comments

0

Try this,

INSERT INTO valias(domain_id, source, destination) 
SELECT id,'canto', 'george' 
FROM aliases 
    JOIN domains AS vdomains 
        ON aliases.domain_id=vdomains.id 
WHERE  vdomains.name =     "domain_name";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.