Mysql error 1111 in one version of query and error 1054 in another - sql

I have two tables:
books: [isbn, book_title, publisher, ...]
inventory: [isbn, date, num_changed]
I want to return book titles for those which are on stock. I tried a join (query 1) and got 1054 error, then I substituted the reference with the literal value and now I get 1111 error.
query 1:
SELECT `books`.`isbn`, `books`.`book_title`, SUM( `inventory`.`numbers_changed` ) AS `num`
FROM `books`
INNER JOIN `inventory` ON `books`.`isbn` = `inventory`.`isbn`
WHERE `books`.`publisher` LIKE '%pint%'
AND `num` > '0'
query 2:
SELECT `books`.`isbn`, `books`.`book_title`, SUM( `inventory`.`numbers_changed` )
FROM `books`
INNER JOIN `inventory` ON `books`.`isbn` = `inventory`.`isbn`
WHERE `books`.`publisher` LIKE '%print%'
AND SUM( `inventory`.`numbers_changed` ) > '0'
What's the correct query to use?
Edit
Here are the create table queries:
CREATE TABLE IF NOT EXISTS `books` (
`isbn` varchar(30) CHARACTER SET ascii NOT NULL,
`book_title` varchar(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`date_published` varchar(10) CHARACTER SET ascii NOT NULL,
`author` varchar(40) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`translator` varchar(40) CHARACTER SET utf8 COLLATE utf8_persian_ci DEFAULT NULL,
`publisher` varchar(50) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`ganre` varchar(50) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`price` int(7) unsigned NOT NULL,
`cover_pic` int(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`isbn`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `inventory` (
`isbn` varchar(30) CHARACTER SET ascii NOT NULL,
`date` varchar(10) CHARACTER SET ascii NOT NULL,
`numbers_changed` int(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

The 1054 error is about referencing a column that doesn't exist. The actual error message would help to know what is causing the issue.
The 1111 error is because you're trying to use aggregate function (in this case, SUM) in the WHERE clause:
WHERE ...
AND SUM( `inventory`.`numbers_changed` ) > '0'
^
|__ see this?
...outside of a subquery. SQL statements are checked from bottom to top, so I expect that removing the SUM in the WHERE clause will show that the 1054 error is still unaddressed.

use having for second where argument
WHERE `books`.`publisher` LIKE '%print%'
HAVING ( COUNT(`inventory`.`numbers_changed`) > '0')
instead of
WHERE `books`.`publisher` LIKE '%print%'
AND SUM( `inventory`.`numbers_changed` ) > '0'

Related

What is wrong with this access query?

Running the below query returns 0 records, but I would expect it to return 3.
SELECT
ID,
DW2_TV_DimStation_Id,
DW2_OTT_DimStation_Id,
Name,
CoreTVCode,
CoreOTTCode,
StrataTVCode,
HouseHolds,
MaleSkew,
FemaleSkew,
AverageAge,
AverageIncome,
BroadReach,
Description,
Owner,
Notes,
timestamp,
CreatedOn,
ModifiedOn,
Retired,
1 AS Accepted
FROM
Planning_DimStation AS src
WHERE
src.[timestamp] = (
SELECT
MAX([timestamp])
FROM
Planning_DimStation AS src2
WHERE
src2.[ID] = src.[ID]
)
AND NOT EXISTS (
SELECT
1
FROM
DimStation AS tgt
WHERE
tgt.[ID] = src.[ID]
);
The part that breaks it is the NOT EXISTS statement. If I delete the NOT EXISTS it works fine.
Table 1: Planning_DimStation
Is an SQL table linked with 3 records in it. Source below.
Table 2: DimStation
Is an Access table (pic of source UI below) that is empty
Could this be a silent fail caused by type missmatch?
Table 1:
CREATE TABLE [Planning].[DimStation]
(
[ID] INT PRIMARY KEY,
[DW2_TV_DimStation_Id] INT NULL,
[DW2_OTT_DimStation_Id] INT NULL,
[Name] NVARCHAR(128) NOT NULL,
[CoreTVCode] CHAR(5) NULL,
[CoreOTTCode] CHAR(10) NULL,
[StrataTVCode] CHAR(10) NULL,
[HouseHolds] DECIMAL(5,2) NULL,
[MaleSkew] DECIMAL(5,2) NULL,
[FemaleSkew] DECIMAL(5,2) NULL,
[AverageAge] INT NULL,
[AverageIncome] DECIMAL(23,2) NULL,
[BroadReach] BIT NULL,
[Description] NVARCHAR(MAX) NULL,
[Owner] NVARCHAR(128) NULL,
[Notes] NVARCHAR(MAX) NULL,
[timestamp] timestamp NOT NULL,
[CreatedOn] DATETIME2(7) CONSTRAINT [df_Planning_DimStation_CreatedOn] DEFAULT (sysutcdatetime()) NOT NULL,
[ModifiedOn] DATETIME2(7) CONSTRAINT [df_Planning_DimStation_ModifiedOn] DEFAULT (sysutcdatetime()) NOT NULL,
[Retired] BIT CONSTRAINT [df_Planning_DimStation_Retired] DEFAULT (0) NOT NULL
)
GO
Table 2:
Joining on different data types tends to yield unexpected results.
To fix this, use casts.
A note is that Access doesn't allow nulls to be cast. So we need to work around that using Nz (same as ISNULL in T-SQL) and explicitly handling nulls.
AND NOT EXISTS (
SELECT
1
FROM
DimStation AS tgt
WHERE
CLng(IIF(tgt.[ID] IS NULL, 0, tgt.ID)) = src.[ID] AND NOT tgt.ID IS NULL
);

Error when creating multiple columns in one table with same values

When I try to exeecute the folliwng in SQL, I get error, I am trying to add multiple columns with same value to one table.
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
), `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
), `Brand_Name` varchar(255) NOT NULL
), `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
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 ' Vendor_SKU_or_Stock_Number varchar(255) NOT NULL
), Brand_Name varchar(255)' at line 13
Please help
You have too many closing parenthesis, and your table name and field names should not be quoted.
Try this:
CREATE TABLE IF NOT EXISTS vendor (
product_id varchar(255) NOT NULL
, Vendor_SKU_or_Stock_Number varchar(255) NOT NULL
, Brand_Name varchar(255) NOT NULL
, Image_URL5 varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Removed ")"
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
, `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
, `Brand_Name` varchar(255) NOT NULL
, `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Try this
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
, `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
, `Brand_Name` varchar(255) NOT NULL
, `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Try this
CREATE TABLE IF NOT EXISTS vendor (
product_id varchar(255) NOT NULL,
vendor_SKU_or_Stock_Number varchar(255) NOT NULL,
brand_Name varchar(255) NOT NULL,
image_URL5 varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARACTER SET=latin1;

SQL Alias / WHERE [duplicate]

This question already has answers here:
Using column alias in WHERE clause of MySQL query produces an error
(7 answers)
Closed 7 years ago.
I come up with a dumb question, but i'm stuck since 20min and can't figure out why this isn't working..
SELECT `A`.*, `B`.`sei` AS `seiAlbum` FROM `Lea_Picture` AS `A` LEFT JOIN
`Lea_Album` AS `B` ON `A`.`idAlbum` = `B`.`idAlbum` WHERE (`seiAlbum` LIKE
'%album_1%') ORDER BY `seiAlbum` ASC LIMIT 50
I got a
#1054 - Unknown column 'seiAlbum' in 'where clause'
Related tables :
CREATE TABLE IF NOT EXISTS `Lea_Album` (
`idAlbum` int(11) NOT NULL AUTO_INCREMENT,
`sei` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
`type` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
`width` int(11) DEFAULT NULL,
`height` int(11) DEFAULT NULL,
PRIMARY KEY (`idAlbum`)
);
CREATE TABLE IF NOT EXISTS `Lea_Picture` (
`idPicture` int(11) NOT NULL AUTO_INCREMENT,
`sei` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
`title` varchar(127) COLLATE utf8_unicode_ci DEFAULT NULL,
`order` int(11) DEFAULT NULL,
`pictImage` varchar(127) COLLATE utf8_unicode_ci DEFAULT NULL,
`idAlbum` int(11) DEFAULT NULL,
PRIMARY KEY (`idPicture`)
);
Any clue ?
you cant give alias name in where clause you have to give orignal column name
give orignal column name b.sei in where clause
SELECT A.*, B.sei AS seiAlbum FROM Lea_Picture AS A LEFT JOIN
Lea_Album AS B ON A.idAlbum = B.idAlbum WHERE (B.sei LIKE
'%album_1%') ORDER BY B.sei ASC LIMIT 50
Column alias is applied outside of the query. You can't refer to it in the same query.
So you either need to do
where `B`.`sei` LIKE '%album_1%'
(the same applies to the use in order by of course)
Or you need to wrap the whole query and apply the filter on the nested query:
select *
from
(
SELECT `A`.*, `B`.`sei` AS `seiAlbum` FROM `Lea_Picture` AS `A` LEFT JOIN
`Lea_Album` AS `B` ON `A`.`idAlbum` = `B`.`idAlbum`
) A
WHERE (`seiAlbum` LIKE '%album_1%')
ORDER BY `seiAlbum` ASC LIMIT 50
seiAlbum isn't in-scope inside the nested query, just like B.sei isn't in-scope outside of it.
This is due to the WHERE clause being executed before the SELECT clause.
Try using a sub-query such as;
SELECT * FROM (SELECT `A`.*, `B`.`sei` AS `seiAlbum` FROM `Lea_Picture` AS `A` LEFT JOIN
`Lea_Album` AS `B` ON `A`.`idAlbum` = `B`.`idAlbum`) A
WHERE (`seiAlbum` LIKE
'%album_1%') ORDER BY `seiAlbum` ASC LIMIT 50

Hotel Reservation System Sql Query?

I want to build a Hotel Reservation System. For this system; database is also used fro an other program... But i have problem: before the reservation i want to see which number of rooms type are available for my for my reservation..
My table create sql querys
CREATE TABLE oteldb.oda (
oda_id INT (11) NOT NULL auto_increment,
oda_tip_id INT (11) DEFAULT NULL,
oda_adi VARCHAR (20) DEFAULT NULL,
PRIMARY KEY (oda_id)
)
ENGINE = MyISAM
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;
CREATE TABLE oteldb.tip (
tip_id INT (11) NOT NULL auto_increment,
tip_adi VARCHAR (20) DEFAULT NULL,
PRIMARY KEY (tip_id)
)
ENGINE = MyISAM
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci
ROW_FORMAT = FIXED;
CREATE TABLE oteldb.rezervasyon (
rezervasyon_id INT (11) NOT NULL auto_increment,
rezervasyon_gt DATE DEFAULT NULL,
rezervasyon_ct DATE DEFAULT NULL,
rezervasyon_oda_id INT (11) DEFAULT NULL,
PRIMARY KEY (rezervasyon_id)
)
ENGINE = MyISAM
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;
i try this but not work
SELECT
*
FROM
oteldb.tip
WHERE
IN tip.tip_id
(SELECT
oteldb.oda.oda_tip_id
FROM
oteldb.oda
WHERE
IN oda.oda_id note
(SELECT
oteldb.rezervasyon.rezervasyon_oda_id
FROM
oteldb.rezervasyon
WHERE
"2012-01-03" BETWEEN AND rezervasyon_ct rezervasyon_gt
AND "2012-01-22" AND BETWEEN rezervasyon_gt rezervasyon_ct))
thanks now...
Assuming that available rooms are those that are not already reserved at any time during the query period, and that rezervasyon_gt and rezervasyon_ct are the reservation start and end dates respectively, try:
select t.tip_adi, count(oda.oda_tip_id)
from oteldb.tip t
left join (select oda_tip_id
from oteldb.oda o
where not exists
(select null
from oteldb.rezervasyon r
where r.rezervasyon_oda_id = o.oda_id and
r.rezervasyon_gt <= '2012-01-22' and
'2012-01-03' <= r.rezervasyon_ct)
) oda on oda.oda_tip_id = t.tip_id
group by t.tip_adi
select
RoomType.tip_adi,
sum( if( Rooms.oda_id = BookedRooms.rezervasyon_oda_id, 0, 1 ) as AvailableCount
from
oteldb.oda Rooms
LEFT JOIN ( select distinct
res.rezervasyon_oda_id
from
oteldb.rezervasyo res
where
res.rezervasyon_gt between '2012-01-22' and '2012-01-03'
OR res.rezervasyon_ct between '2012-01-22' and '2012-01-03'
) BookedRooms
on Rooms.oda_id = BookedRooms.rezervasyon_oda_id
JOIN oteldb.tip RoomType
on Rooms.oda_tip_id = RoomType.tip_id

How can I insert a new column after x on a table?

I have a table called users:
CREATE TABLE `users` (
`UID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(200) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`email` VARCHAR(255) NOT NULL ,
`phone` VARCHAR(30) NOT NULL ,
`logo` VARCHAR(255) NULL ,
`address` VARCHAR(100) NULL ,
`information` TEXT NULL ,
`announcement` TEXT NULL ,
`role` INT UNSIGNED NULL ,
`yahoo` VARCHAR(100) NULL ,
`twitter` VARCHAR(20) NULL ,
`timelogin` DATETIME NULL,
PRIMARY KEY (`UID`) ,
UNIQUE INDEX `UID_UNIQUE` (`UID` ASC) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
UNIQUE INDEX `logo_UNIQUE` (`logo` ASC) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
the problem is that I want to add or insert a new row like
`image_seal` VARCHAR(255) NULL ,
after logo, can we do something like that in mysql ? append-thing?
Thanks
Adam Ramadhan
ALTER TABLE users ADD COLUMN image_seal VARCHAR(255) NULL AFTER logo;
Check here for Syntax of ALTER TABLE
It seems you want to add a new column to your table, not a row.
From the MySQL reference it seems you can use the keyword AFTER.
ALTER TABLE users ADD COLUMN image_seal VARCHAR(255) AFTER logo;
should do the trick.
What are you using to administer your database? SQLBuddy, PHPmyAdmin and others can do this for you, without you needing to run queries.