Select only the domain name from an email address - mysql

I'm trying to get the domain instead of the full e-mail address in my query for Domain_. In the statement below I select user_data. Email_addressbut that provides foo#bar.com instead of bar.com which isn't sufficient:
INSERT INTO user_orders
(`User_ID`, `Order_Number`, `Customer_ID`, `Email_address`, `Domain_`)
SELECT
user_data.`User_ID`, order_data.`Order_Number`, user_data.`Customer_ID`,
user_data.`Email_address`, user_data.`Domain_`
FROM user_data
INNER JOIN order_data ON order_data.User_ID = user_data.User_ID;
My understanding was SUBSTRING_INDEX(Email_address,'#',-1) as Domain_ would work so I tried:
INSERT INTO user_orders
(`User_ID`, `Order_Number`, `Customer_ID`, `Email_address`, `Domain_`)
SELECT
user_data.`User_ID`, order_data.`Order_Number`, user_data.`Customer_ID`, user_data.`Email_address`,
(SUBSTRING_INDEX(SUBSTR(user_data.`Email_address`, INSTR(user_data.`Email_address`, '#') + 1),'.',1)) as user_data.`Domain_`
FROM user_data
INNER JOIN order_data ON order_data.User_ID = user_data.User_ID;
but the query fails and provides a syntax error.
To help isolate it more for clarity this is what I'm doing differently:
(SUBSTRING_INDEX(SUBSTR(user_data.`Email_address`, INSTR(user_data.`Email_address`, '#') + 1),'.',1)) as user_data.`Domain_`
Any help would be appreciated as I'm stuck.
Error: INSERT INTO user_orders(User_ID, Order_Number, Customer_ID, Email_address, Domain_)
select user_data.User_ID, order_data.Order_Number, user_data.Customer_ID, user_data.Email_address, (SUBSTRING_INDEX(SUBSTR(user_data.Email_address, INSTR(user_data.Email_address, '#') + 1),'.',1)) as user_data.Domain_ FROM user_data
INNER JOIN order_data on order_data.User_ID = user_data.User_ID;
/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.Domain_ FROM user_data
INNER JOIN order_data on order_data.User_ID = user...' at line 2 */```

If you execute for example
SELECT split_part(email, '#', 2) AS Domain FROM user_orders;
It will show everything after the '#' which is the domain you want.
All info
Now, using the query

Related

Problem with syntax when try add trigger in mysql

I need help with my SYNTAX error
I try to add trigger into mysql by still have syntax error.
I will be glad for any help
When I try add this trigger i still have error 1064
Full error response is: Error Code: 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 '' at line 19 MySQL version is 5.7
CREATE TRIGGER delete_przejazdy_logger AFTER DELETE ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 1, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
OLD.data, OLD.osob, OLD.fotelik, OLD.detale, OLD.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON OLD.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON OLD.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON OLD.id_adres_z = adresy_z.id
WHERE pasazerowie.id = OLD.id_pasazera);
END;
I add triggers on this same db for logs on creating records and works fine.
Here is the code which works good:
CREATE TRIGGER add_przejazdy_logger AFTER INSERT ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 2, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
NEW.data, NEW.osob, NEW.fotelik, NEW.detale, NEW.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON NEW.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON NEW.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON NEW.id_adres_z = adresy_z.id
WHERE pasazerowie.id = NEW.id_pasazera);
END;

SQL nested queries and subqueries

i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)
I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);

MySQL query not working, syntax error near INNER JOIN

Not done any programming for a few years so am somewhat rusty again.
I have two tables users table and users_profiles table. I want to update users_profiles table on the condition that user_uid matches on both tables (users.user_uid and users_profiles.user_uid) and where users.user_login = 'johndoe' and users.user_uid = '11'.
It will be executed in a php script, so johndoe would actually be users username (whatever is stored in session and same for users_uid. For simplicitity i added dummy data.
I run the query in phpmyadmin and get syntax error near INNER JOIN. I just cannot figure out what i'm doing wrong (probably wrote it entirely wrong) and have spent few hours trying to work it out without success.
Heres my sql query.
UPDATE
users_profiles
SET
users_profiles.user_fname = 'John',
users_profiles.user_lname = 'Doe',
users_profiles.user_gender = 'male'
INNER JOIN
users
ON
users.user_uid = users_profiles.user_uid
WHERE
users.user_login = 'johndoe'
AND
users.user_uid = '11'
error i get when running sql query via phpmyadmin.
#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 'ON users.user_uid, .user_uid FROM users_profiles WHERE
users.user_login ' at line 7
Thanks.
Try this:
UPDATE
users_profiles
INNER JOIN
users
ON
users.user_uid = users_profiles.user_uid
SET
users_profiles.user_fname = 'John',
users_profiles.user_lname = 'Doe',
users_profiles.user_gender = 'male'
WHERE
users.user_login = 'johndoe'
AND
users.user_uid = '11'
Try to exchange SET and INNER JOIN as shown, for example, in this SO answer:
UPDATE
users_profiles p
INNER JOIN
users u
ON
u.user_uid = p.user_uid
SET
p.user_fname = 'John',
p.user_lname = 'Doe',
p.user_gender = 'male'
WHERE
u.user_login = 'johndoe'
AND
u.user_uid = 11

Yii2: can't create sql command using innerJoin

I am not that good at SQL but i do as much as i can for the little knowledge i have..
I have made a single a flat SQL string with the help from a friend that gathers data from a table using a relative table from an initial data from the first table, the SQL was made like this:
SELECT id, username, auth_assignment.created_at
FROM `user` JOIN `auth_assignment`
ON (user.id = auth_assignment.user_id)
JOIN `auth_item`
ON (auth_item.name = auth_assignment.item_name)
WHERE auth_item.name = 'Admin'
the initial data to look is Admin so everything works in that side, but i tried to simulate this SQL using Yii2 functions.. so far i have made this code
$query = new Query;
$command = $query->select('id, username')
->innerJoin('auth_assignment', ['user.id'=>'auth_assignment.user_id'])
->innerJoin('auth_item', ['auth_item.name'=>'auth_assignment.item_name'])
->where('auth_item.name = :name', [':name'=>$name])->createCommand();
var_dump($command->query());
this returns an SQLSTATE error:
SQLSTATE[42000]: Syntax error or access violation: 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 `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN' at line 1
The SQL being executed was: SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN `auth_item` ON `auth_item`.`name`='auth_assignment.item_name' WHERE auth_item.name = 'Admin'
i checked the method $command->sql; to know how the SQL was being generated.. but i really don't know how to fix it due to my lack of my knowledge to SQL and lack of understanding to yii2 api documentation
SQL is generated like this:
SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`=:qp1 INNER JOIN `auth_item` ON `auth_item`.`name`=:qp2 WHERE auth_item.name = :name
i appreciate any help
Try This Query
$query = (new yii\db\Query())
->select('id, username, auth_assignment.created_at')
->from('user')
->innerJoin('auth_assignment','user.id=auth_assignment.user_id')
->innerJoin('auth_item','auth_item.name = auth_assignment.item_name')
->where([
'auth_item.name' => 'Admin'
])->all();

create mysql views with UNION operator on query

I am trying to create sql view with a UNION operator on it. I tried executing the sql first before embedding it to the view and the result is success. But when I try it to embed on creating a view it returned this error
"Error Code : 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 'UNION
SELECT p.product_media_id AS media_id, p.product_title AS title, p.product' at line 8
"
I also assure that all columns have the same data type.
SQL view:
CREATE
/*[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]*/
VIEW `mydbname`.`s_views`
AS
(SELECT c.media_id AS media_id,
c.title AS title,
c.title_slug AS slug,
c.content_one AS description,
c.type AS cat
FROM content_content c
WHERE c.type = 'news'
OR c.type='travel_genius'
AND c.media_id IS NOT NULL
AND c.status = 1
UNION
SELECT p.product_media_id AS media_id,
p.product_title AS title,
p.product_title_slug AS slug,
p.product_description AS description,
'product' AS cat
FROM p_views p
WHERE p.product_media_id IS NOT NULL);
It is a bug in MySQL. It is the parenthesis that is triggering it:-
http://bugs.mysql.com/bug.php?id=21614