I have to "port" an SQLite database to MySQL. The database is set up from a C# application with the Connector/NET lib.
Porting the methods for creating the tables of the DB was straight forward, but I got stuck at a view.
The SQLite code for the view works just fine and looks like the following:
CREATE VIEW IF NOT EXISTS EXO_ResultsSummaries AS
SELECT ParticipantBIB,
t.HeatIndex,
(CASE WHEN t.QualiGroup IS NULL THEN t.Finals ELSE t.QualiGroup END) AS HeatLevel,
PointsObstacle0 + PointsObstacle1 AS TotalScore, TimeObstacle0 + TimeObstacle1 AS TotalTime
FROM (EXO_Results INNER JOIN Heats ON EXO_Results.HeatIndex = Heats.HeatIndex) t
WHERE t.Completed = 1;
Since the "IF NOT EXISTS" statement does not work with MySQL, I changed the code to the following:
CREATE OR REPLACE VIEW EXO_ResultsSummaries AS
SELECT ParticipantBIB,
t.HeatIndex,
(CASE WHEN t.QualiGroup IS NULL THEN t.Finals ELSE t.QualiGroup END) AS HeatLevel,
PointsObstacle0 + PointsObstacle1 AS TotalScore, TimeObstacle0 + TimeObstacle1 AS TotalTime
FROM (EXO_Results INNER JOIN Heats ON EXO_Results.HeatIndex = Heats.HeatIndex) AS t
WHERE t.Completed = 1;
However, if I try to create the view from my application (or from the workbench), I get 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 'AS t WHERE t.Completed = 1' at line 6
Thanks to Shadow for his comments. Changed my code accordingly and this worked just fine:
CREATE OR REPLACE VIEW EXO_ResultsSummaries AS SELECT ParticipantBIB,
Heats.HeatIndex,
(CASE WHEN Heats.QualiGroup IS NULL THEN Heats.Finals ELSE Heats.QualiGroup END) AS HeatLevel,
PointsObstacle0 + PointsObstacle1 AS TotalScore, TimeObstacle0 + TimeObstacle1 AS TotalTime
FROM EXO_Results INNER JOIN Heats ON EXO_Results.HeatIndex = Heats.HeatIndex
WHERE Heats.Completed = 1;
Related
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.
The query below works fine and is what I need.
I want to add a new keyword onto "Keywords" e.g.
UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess')
However no matter where I place this in the logic below, I get a syntax error.
The web examples I have seen and in Stackoverflow are for simple
Update ... WHERE .... examples.
SET #StartDate = '2016-03-01';
SET #EndDate = '2016-03-31';
SELECT
bugs_activity.bug_id,
bug.status_whiteboard AS Whiteboard,
bug.keywords AS Keywords,
bug.bug_status,
bug.resolution,
SUM(CASE WHEN fd.name = 'bug_status' AND (bugs_activity.added = 'VERIFIED' OR bugs_activity.added = 'CLOSED') THEN 1 ELSE 0 END) AS ClosedCount,
MIN(CASE WHEN fd.name = 'bug_status' AND bugs_activity.added = 'VERIFIED' THEN bug_when ELSE NULL END) AS verifiedDate,
MIN(CASE WHEN fd.name = 'bug_status' AND bugs_activity.added = 'CLOSED' THEN bug_when ELSE NULL END) AS closedDate
FROM bugs_activity
INNER JOIN bugs bug
ON bugs_activity.bug_id = bug.bug_id
INNER JOIN fielddefs fd
ON bugs_activity.fieldid = fd.id
WHERE
(bugs_activity.bug_when BETWEEN '2015-09-01' AND #EndDate)
AND (Keywords LIKE '%Region:Europe%')
AND NOT (Keywords LIKE '%Report:DevProcess%')
GROUP BY bug_id
HAVING
ClosedCount > 0
AND (
(verifiedDate IS NOT NULL AND verifiedDate >= #StartDate)
OR (verifiedDate IS NULL AND (closedDate IS NOT NULL AND closedDate >= #StartDate))
)
Additional info from questions:
Linqpad SQL talking to MySQL DB
From linqpad - 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 'UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess')' at line 20
I removed GroupBy line, same error.
CONCAT is what a web search tells me How to prepend a string to a column value in MySQL?
In the selected "bugs" I want to "UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess') "
Bugzilla has historically for a long time used multiple keywords. It is what it is whether good or bad.
== 31/05/2016 update ==
I simplified the query and got past the syntax error, however no update. I confirmed account had DB write access by using the read account which produced an access denied error.
-- this shows the one record
SELECT bug_id
FROM bugs
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')
-- this runs without an error but shows no records updated
UPDATE bugs SET bugs.keywords = CONCAT(bugs.keywords, ', Report:DevProcess')
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')
The simpler syntax does "work", i.e. no syntax error however the write did not work initially. This turned out to be because the keyword schema required the keyword to be pre-defined. Adding the keyword in resulting in the record being updated.
-- this runs without an error but shows no records updated
UPDATE bugs SET bugs.keywords = CONCAT(bugs.keywords, ', Report:DevProcess')
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')
Hi I'm trying to write a cursor in powerbuilder 12 to fetch some records . Here this is my query.
I'm trying to fetch some records from the trninvhdr table which are not in the second table.
SELECT
INV_DATE,
INV_NO,
INV_TYPE,
CUR_CODE,
EXCH_RATE,
usd_rate,
CR_TERM,
DUE_DATE,
bl_date,
TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE ='NFL1' AND
CUST_CODE = 'NLML' AND
INV_TYPE ='F' AND
INV_DATE <= '2016-03-25' AND
NOT EXISTS
(SELECT * FROM trninvoiceavailability WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
Here how I use it in the program.
DECLARE lc_retrieve CURSOR FOR
SELECT
trninvhdr.INV_DATE,
trninvhdr.INV_NO,
trninvhdr.INV_TYPE,
trninvhdr.CUR_CODE,
trninvhdr.EXCH_RATE,
trninvhdr.usd_rate,
trninvhdr.CR_TERM,
trninvhdr.DUE_DATE,
trninvhdr.bl_date,
trninvhdr.TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE = :as_comp_code AND
CUST_CODE = :as_cust_code AND
INV_TYPE ='F' AND
INV_DATE <= :as_inv_date )AND
NOT EXISTS (SELECT * FROM trninvoiceavailability
WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND
trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
open lc_retrieve ;
The query works fine in the mysql server but in the progra it gives me the following error .
Database c0038 SQLSTATE = 3700 MySQL ODBC 5.2 a Driver mysql id 5.5.25 You have an error in your syntax. check the manual that corresponds to your mysql version for the right syntax to use near NOT EXISTS (SELECT * FROM trninvoiceavailability.... at line 1.
What is the correct Syntax that I should use to work this query.
I can see a bracket in this code... where did it come from and where is it's friend?
INV_DATE <= :as_inv_date )AND
You need to remove ) from your query as per below-
INV_DATE <= :as_inv_date )AND
sholuld be INV_DATE <= :as_inv_date AND
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();
Why Can't I use or why does it encounters an error. I'm trying to use a script as such as the one below. I'm a newbie when it comes to mysql and I tried creating scripts like this but I get the errr like the below
#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 '1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.statu' at line 4
Here's my script
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount =
CASE WHEN dogdetails.dogcount IS NOT 1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.status = 'Checked'
WHEN dogdetails.dogcount = 1 THEN dogs.pto = pto - dogdetails.total, dogdetails.status = 'Checked'
WHERE dogdetails.id=4
Is there somethng I'm missing or overlooked?
remember that you have to use the keyword 'end' after your case statements and to always return a value. The IS NOT keyword is also incorrect when comparing numbers. Here's a version of your query that should work:
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount = CASE WHEN dogdetails.dogcount != 1 THEN dogs.dogcount = dogcount-dogdetails.total else dogs.dogcount end,
dogs.pto= case WHEN dogdetails.dogcount = 1 THEN pto - dogdetails.total else pto end,
dogdetails.status = 'Checked'
WHERE dogdetails.id=4