create mysql views with UNION operator on query - mysql

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

Related

Select only the domain name from an email address

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

How to implement "SHOW COLUMN FROM (SELECT Result) WHERE Column_name = 'column_name'"?

Good Evning My Brothers/Sisters.
I want to check whether a column is exist from select result.
I have tried some effort like below but instead it gives me an error.
SHOW COLUMNS
FROM (SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS sudah_isi, SUM(hasil.belum_isi) AS belum_isi
FROM
(
SELECT
a.id_pegawai, c.skpd, c.kode_skpd, a.id_satuan_organisasi, a.nama_pegawai, a.nip,
CASE
WHEN i.id_pegawai IS NULL THEN 0
ELSE 1
END AS sudah_isi,
CASE
WHEN i.id_pegawai IS NULL THEN 1
ELSE 0
END AS belum_isi
FROM
tbl_pegawai a
LEFT JOIN ref_skpd c ON a.id_satuan_organisasi = c.id_skpd
LEFT JOIN tbl_bapertarum i ON (a.id_pegawai = i.id_pegawai AND YEAR(i.tgl_lapor)='2015')
GROUP BY
a.id_pegawai
) hasil
WHERE
1 AND hasil.kode_skpd LIKE 'Badan Kepegawaian Daerah%'
GROUP BY
hasil.id_satuan_organisasi
ORDER BY
hasil.kode_skpd) WHERE Field = "kode_skpd"
This is the full error result:
#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
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS s' at line 2
How do i to find the existence of a column or columns from SELECT result ?
Thanks in advance Brothers/Sisters.

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')
);

Can't create a MySQL View

I run this SQL code to create a view but this doesn't work :
Create VIEW as
select
c.ID_CAPTEUR as ID_CAPTEUR,
d.ID_CAPTURE as ID_CAPTURE,
d.VALEUR_CAPTURE as valeur,
d.DATE_CAPTURE,
t.INTITULE_TYPE_CAPTURE as TYPE_CAPTURE,
z.INTITULE_ZONE as zone,
r.INTITULE_REGION as region
from CAPTEUR c, CAPTURE d, TYPECAPTURE t, ZONE z, REGION r
where c.ID_CAPTEUR=d.ID_CAPTEUR
and d.ID_TYPE_CAPTURE=t.ID_TYPE_CAPTURE
and c.ID_ZONE = z.ID_ZONE
and z.ID_REGION = r.ID_REGION
I get this error :
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 'as select c.ID_CAPTEUR as ID_CAPTEUR , d.ID_CAPTURE as ID_CAPTURE , d.VALEUR_CAP' at line 1
This is the syntax of the CREATE VIEW statement:
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
As you can see, your query is missing the view_name parameter (parameters between [ and ] are optionnal, which is not the case of view_name).

Update my database from Select result

mysql> SELECT Ext, Pass, Name, Context FROM temp_Users WHERE temp_Users.Pass NOT IN (SELECT Pass FROM Users);
+------+-------+---------+------------+
| Ext | Pass | Name | Context |
+------+-------+---------+------------+
| 6003 | Hello | WebPone | DLPN_Admin |
+------+-------+---------+------------+
1 row in set (0.00 sec)
mysql> UPDATE Users
-> SET (Pass, Name, Context) = (SELECT Pass, Name, Context FROM temp_Users WHERE temp_Users.Pass NOT IN (SELECT Pass FROM Users))
-> WHERE Users.Ext = temp.Ext;
ERROR 1064 (42000): 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 '(Pass, Name, Context) = (SELECT Pass, Name, Context FROM temp_Users WHERE temp_' at line 2
I want to update my database from Select result and i am getting this error. Please tell me how i can resolve it ?
MySQL doesn't support the SET ( multiple_fields ) = ( subquery_that_returns_multiple_fields ) syntax for UPDATE statements. Instead, you have to use a "multiple-table" update (a join). See http://dev.mysql.com/doc/refman/5.6/en/update.html.
Your query has some other problems as well, so I'm not clear on exactly what you want . . . but I think you want something like this:
UPDATE users
JOIN temp_users
ON temp_users.ext = users.ext
SET users.pass = temp_users.pass,
users.name = temp_users.name,
users.context = temp_users.context
WHERE temp_users.pass NOT IN
-- extra subquery to bypass MySQL limitation:
( SELECT pass
FROM ( SELECT pass
FROM users
) t
)
;
UPDATE Users u
JOIN temp_Users tu ON tu.Ext = u.Ext
SET
Pass = tu.Pass,
Name = tu.Name,
Context = tu.Context
WHERE tu.Pass NOT IN (
SELECT Pass
FROM Users
)