Memberships table:
CREATE TABLE `Consultant_Memberships` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`membership_url` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
Memberships_List table:
CREATE TABLE `Consultant_Memberships_List` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`consultant_id` int(11) DEFAULT NULL,
`membership_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Within the Memberships table, there is a list of 'Societies' which the member can become a part of. On selection, this is then added to the 'Memberships_List' in the form of:
id - Auto increment
consultant_id - The unique ID of the user who's added the societies
membership_id - Refers to the 'id' from the memberships table.
I want to be able to show in a drop down list only the memberships which the user hasn't chosen yet. So far I've got:
$query = $db->query("SELECT `Consultant_Memberships.`id`, `Consultant_Memberships`.`title` `FROM `Consultant_Memberships
WHERE NOT EXISTS (SELECT `Consultant_Memberships`.`id`, `Consultant_Memberships`.`title`
WHERE `Consultant_Memberships`.`id` = $user_id)");
I'm currently getting this error, and also unsure if this is the correct query:
PHP Fatal error: Uncaught exception 'PDOException' with message '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 'WHERE `Consultant_Memberships_List`.`id` = )' at line 1' in /Users/Sites/pages/medical.php:72
Stack trace:
#0 /Users/Sites/pages/medical.php(72): PDO->query('SELECT `Consult...')
#1 /Users/Sites/index.php(18): include('/Users/Site...')
#2 {main}
thrown in /Users/Sites/pages/medical.php on line 72
FROM is missing in the NOT EXISTS subquery.
SELECT `Consultant_Memberships.`id`, `Consultant_Memberships`.`title` `FROM `Consultant_Memberships
WHERE NOT EXISTS (SELECT `Consultant_Memberships`.`id`, `Consultant_Memberships`.`title`
WHERE `Consultant_Memberships`.`id` = $user_id)
you have a wrong syntax try something like this i am not writing exect query but checked in sql fiddle and thats wrond
SELECT Consultant_Memberships.id, Consultant_Memberships.title FROM Consultant_Memberships
WHERE NOT EXISTS (SELECT Consultant_Memberships.id, Consultant_Memberships.title from Consultant_Memberships
WHERE Consultant_Memberships.id = 1)
Always watch to the left from the highlighted query part. WHERE in your case
SELECT `Consultant_Memberships.`id`, `Consultant_Memberships`.`title`
`FROM <--- extra backtick
`Consultant_Memberships <--- unclosed backtick
by the way, do not overuse backticks. most of your fields require them not
...and you have your query totally screwed as it seems
As far as I understood your question, it have to be
SELECT cm.id, title
FROM Consultant_Memberships cm LEFT JOIN Consultant_Memberships_List
ON cm.id=membership_id WHERE membership_id IS NULL
Please note that your question has nothing to do with PDO.
it's clear SQL query question.
Kindly try this one:
SELECT a.id, a.title
FROM Consultant_Memberships a, Consultant_Memberships_List b
WHERE a.id <> b.consultant_id
The reason you are getting syntax error because if you see your subquery you will find out that you are not specifying any table in it.
WHERE NOT EXISTS (SELECT `Consultant_Memberships`.`id`, `Consultant_Memberships`.`title`
WHERE `Consultant_Memberships`.`id` = $user_id)
If you need more help, please let us know...
Regards...
Mr.777
Related
So I wanted to add new column to my table made from a select using two other tables. I tried a query like this:
ALTER TABLE order ADD cost DECIMAL(5,2) AS (SELECT SUM(price*pieces) FROM book JOIN details ON id_book=book_id GROUP BY order_id);
And I get error:
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 'select sum(price*pieces) from book join details on id_book=book_id group by order_id ' at line 1
My other tables look like this:
CREATE TABLE details (
id_d INT(10) NOT NULL AUTO_INCREMENT,
book_id INT(10) DEFAULT NULL,
order_id INT(10) DEFAULT NULL,
pieces INT(10) DEFAULT NULL
...
CREATE TABLE book (
id_book INT(10) NOT NULL AUTO_INCREMENT,
price DECIMAL(5,2) DEFAULT NULL,
...
This
SELECT SUM(price*pieces) FROM book JOIN details ON id_book=book_id GROUP BY order_id; works but I really don't know how to add this as a new column :(
You can't specify the data to fill the column in the ALTER TABLE statement. That needs to be done in a separate UPDATE statement.
ALTER TABLE order ADD cost DECIMAL(5,2) DEFAULT 0;
UPDATE order AS o
JOIN (
SELECT d.order_id, SUM(d.pieces, * b.price) AS cost
FROM details AS d
JOIN book AS b ON d.book_id = b.id_book
GROUP BY d.order_id) AS d ON d.order_id = o.order_id
SET o.cost = d.cost
Question's been posted for some time now and the funny thing is I also thought syntax was a bit odd when I saw it in my project and I ended up here looking for some explanation. I understand now that while it may not be possible to use both ALTER TABLE and a fill-in value statement, one can declare a column field as a 'COMPUTED' type meaning you can declare a function that will execute on the fly in every select statement, I leave here a sample code reference using this syntax for whoever finds it useful:
ALTER TABLE ACCOUNTS ADD ACCOUNT_CASH AS get_VALUE ('CASH', CURRENCY, BUSINESS_TYPE, STATUS );
Though in most of the cases, a trigger would be a better approach.
I have this query:
SELECT `Stocks`.`id` AS `Stocks.id` , `Locations`.`id` AS `Locations.id`
FROM `rowiusnew`.`c_stocks` AS `Stocks`
LEFT JOIN `rowiusnew`.`g_locations` AS `Locations` ON ( `Locations`.`ref_id` = `Stocks`.`id` AND `Locations`.`ref_type` = 'stock' )
GROUP BY `Stocks`.`id`
HAVING `Locations.id` IS NOT NULL
This returns 0 results.
When I add
ORDER BY Locations.id
to the exactly same query, I correctly get 3 results.
Noteworthy: When I discard the GROUP BY clause, I get the same 3 results. The grouping is necessary for the complete query with additional joins; this is the simplified one to demonstrate the problem.
My question is: Why do I not get a result with the original query?
Note that there are two conditions in the JOIN ON clause. Changing or removing the braces or changing the order of these conditions does not change the outcome.
Usually, you would suspect that the field id in g_locations is sometimes NULL, thus the ORDER BY clause makes the correct referenced result be displayed "at the top" of the group dataset. This is not the case; the id field is correctly set up as a primary key field and uses auto_increment.
The EXPLAIN statement shows that filesort is used instead of the index in those cases when I actually get a result. The original query looks like this:
The modified, working query looks like this:
Below is the table definitions:
CREATE TABLE IF NOT EXISTS `c_stocks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_stock_type` int(10) unsigned DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`locality` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `StockType_idx` (`id_stock_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `g_locations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`ref_type` enum('stock','object','branch') DEFAULT NULL,
`ref_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UniqueLocation` (`ref_type`,`ref_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The ref_id field features a long comment that I omitted in this definition.
After being unable to reproduce the error on SQLFiddle.com and also on my second computer, I realized that there must be a bug involved.
Indeed, my used version 5.6.12 suffers from this bug:
Some LEFT JOIN queries with GROUP BY could return incorrect results. (Bug #68897, Bug #16620047)
See the change log of MySQL 5.6.13: http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-13.html
An upgrade to 5.6.17 solved my problem. I am not getting the results I expect, independent of ORDER clauses and aggregate functions.
Remove
having locations.id is not null
instead use
where locations.id is not null
locations.id is not null is not a problem for the grouping - you don't want them included at all.
Also, you need to do something with the locations.id since it isn't in the group by clause. Do you want "max" locations.id?
so your query now becomes:
SELECT `Stocks`.`id` AS `Stocks.id` , max(`Locations`.`id`) AS `Locations.id`
FROM `rowiusnew`.`c_stocks` AS `Stocks`
LEFT JOIN `rowiusnew`.`g_locations` AS `Locations` ON ( `Locations`.`ref_id` = `Stocks`.`id` AND `Locations`.`ref_type` = 'stock' )
WHERE `Locations.id` IS NOT NULL
GROUP BY `Stocks`.`id`
Make those changes and it should work better for you.
FYI: I think that by putting in the order by clause, you are allowing the engine to guess what you want for the locations.id, otherwise it has no clue. In something other than MYSQL, it wouldn't run at all.
The following SQL statement is valid and executes as expected:
SELECT r1.rs_objectidentifier AS parent,
r2.rs_objectidentifier AS child,
"satisfies" AS relation,
0 AS root,
0 AS leaf
FROM rsdata r1,
rsdata r2
WHERE r1.weeknumber="1410"
AND r1.weeknumber=r2.weeknumber
AND r1.rs_variant=r2.rs_variant
AND r1.rs_inrsobjectidentifieronelevel != ""
AND r1.rs_inrsobjectidentifieronelevel != "Unknown"
AND find_in_set(r2.rs_objectidentifier, r1.rs_inrsobjectidentifieronelevel)
UNION
SELECT rsdata.rs_objectidentifier AS parent,
"" AS child,
"satisfies" AS relation,
0 AS root,
0 AS leaf
FROM rsdata
WHERE rsdata.weeknumber="1410"
AND (rsdata.RS_InRSObjectIdentifierOneLevel = ""
OR rsdata.RS_InRSObjectIdentifierOneLevel = "Unknown")
when I prepend the select with the following insert statement, I get an SQL syntax error:
insert into traceability
The columns in the select match the fields in the table (as far as I can tell). The table looks like:
CREATE TABLE `traceability` ( `Parent` varchar(50) default NULL,
`Child` varchar(50) default NULL, `Relation` varchar(20) default
NULL, `Root` tinyint(1) default NULL, `Leaf` tinyint(1) default
NULL, UNIQUE KEY `Traceability_UI1` (`Parent`,`Child`,`Relation`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1
Why do I get a MySQL syntax error when the insert statement as added???
The following error (not very informative) is...
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 'insert into
traceability select r1.rs_objectidentifier as parent, r2.rs_objec' at line 2
Try this
insert into traceability(Parent,Child,Relation,Root,Leaf) Select .....
OK. Solution was very simple. The SQL is part of a set of SQL statements. The statement posted above was missing a ; (to terminate the statement) so was running into the next statement and therefore throwing a syntax error.... hangs head in shame
MySQL Version 5.0.67
Take a look at this very simple table and tell me if I have found a MySQL bug, I have tried to search for an answer but as you can imagine it's a bit hard to come up with the right search terms
CREATE TABLE `product` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `product` VALUES (1, 'jim');
INSERT INTO `product` VALUES (2, 'bob');
From there I can then select the following
SELECT * FROM `product` WHERE `id` = '1';
Obviously this returns a row, but then, so does this
SELECT * FROM `product` WHERE `id` = '1blah';
Erm... WHY? Surely this is wrong or am I going mad? Will crawl the web a bit more before I file a bug report with MySQL.
It's automatically converting the string "1blah" into an integer. As the string begins with a "1" the resultant integer is simply 1.
As such, it's just trying to do the right thing, even though it might seem a bit counter-intuitive.
This happens because of type conversion. Since your column has integer value, '1blah' is converted to 1. Please, see http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html for more details.
If you didn't enclose the integer id in quotes, it'd work and you'd get an error as you'd hope. That is,
SELECT * FROM `product` WHERE `id` = 1;
1 row in set
works, while
SELECT * FROM `product` WHERE `id` = 1blah;
ERROR 1054 (42S22): Unknown column '1blah' in 'where clause'
errors.
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
im tearing my hair out over this one. A query is throwing an error:
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 'FROM, SUBJECT, DATE, READ, MAIL ) VALUES ( 'EJackson', 'dfdf', '1270974101', 'fa' at line 1
I printed out the query to see what could be the problem:
INSERT INTO db.tablename ( FROM, SUBJECT, DATE, READ, MAIL ) VALUES ( 'EJackson', 'dfdf', '1270974299', 'false', 'dfdsfdsfd' )
and finaly the structure consists of:
CREATE TABLE db.tablename (
`ID` int(12) NOT NULL auto_increment,
`FROM` varchar(255) NOT NULL,
`SUBJECT` varchar(255) NOT NULL,
`DATE` varchar(255) NOT NULL,
`READ` varchar(255) NOT NULL,
`MAIL` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I can't find anything wrong. Any help would be much appreciated.
In the insert statement, "FROM" is a keyword in SQL so you need to enclose it in backquotes like you have in your create table statement.
So it'll be like:
INSERT INTO db.tablename (`FROM`, `SUBJECT`, `DATE`, `READ`, `MAIL` ) VALUES ( 'EJackson', 'dfdf', '1270974299', 'false', 'dfdsfdsfd' )
Isn't FROM a reserved word in MySQL?