SQL Alias / WHERE [duplicate] - sql

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

Related

Counting forum topics and replies

I have 2 tables: posts and forum_topics. Each post (a reply) is associated with another post (a forum topic, which is then associated with the forum_topics tables).
Problem: I need to count all forum topics and replies in the posts table. This is what I have so far:
SELECT ForumTopic.id, ForumTopic.title, ForumTopic.modified, COUNT(ReplyLeftOuterJoin.id) as replies_count
FROM forum_topics AS ForumTopic
LEFT OUTER JOIN posts AS PostLeftOuterJoin
ON PostLeftOuterJoin.object_id = ForumTopic.id
AND PostLeftOuterJoin.object_type = 'forum_topic'
AND PostLeftOuterJoin.status = 'approved'
LEFT OUTER JOIN posts AS ReplyLeftOuterJoin
ON ReplyLeftOuterJoin.object_id = PostLeftOuterJoin.id
AND ReplyLeftOuterJoin.object_type = 'post'
AND ReplyLeftOuterJoin.status = 'approved'
WHERE ForumTopic.forum_category_id = 'some_id'
Edit
Currently I'm only getting a count of all replies associated with a forum_topic (post) in the posts table. I would like to get a count of forum_topics in posts table associated with a forum topic in the forum_topics table.
NB FYI, a solution to this problem should use one query only.
Here is the schema of the two tables:
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`context_id` bigint(20) unsigned DEFAULT NULL,
`context_type` enum('resource','module','kwik','user','assignment') COLLATE utf8_unicode_ci DEFAULT NULL,
`is_private` tinyint(1) NOT NULL,
`is_unread` tinyint(4) NOT NULL,
`last_replied` datetime NOT NULL,
`object_id` bigint(20) unsigned DEFAULT NULL,
`object_type` enum('forum_topic','forum','user','post') COLLATE utf8_unicode_ci DEFAULT NULL,
`status` enum('approved','unapproved','disabled') COLLATE utf8_unicode_ci NOT NULL,
`post` text COLLATE utf8_unicode_ci NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS `forum_topics`;
CREATE TABLE `forum_topics` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`view_count` int(10) unsigned NOT NULL,
`forum_category_id` bigint(20) unsigned NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SELECT
ForumTopic.forum_category_id,
COUNT(DISTINCT PostLeftOuterJoin.id) as forumtopics_count,
COUNT(ReplyLeftOuterJoin.id) as replies_count
FROM forum_topics AS ForumTopic
LEFT OUTER JOIN posts AS PostLeftOuterJoin
ON PostLeftOuterJoin.object_id = ForumTopic.id
AND PostLeftOuterJoin.object_type = 'forum_topic'
AND PostLeftOuterJoin.status = 'approved'
LEFT OUTER JOIN posts AS ReplyLeftOuterJoin
ON ReplyLeftOuterJoin.object_id = PostLeftOuterJoin.id
AND ReplyLeftOuterJoin.object_type = 'post'
AND ReplyLeftOuterJoin.status = 'approved'
WHERE ForumTopic.forum_category_id = 'some_id'
GROUP BY
ForumTopic.forum_category_id
;
maybe you can get the result in two queries
the following query will give you the count of posts in each forum
select f.id,f.title,f.modified,Type='Posts',Number=count(*)
from forum_topics as f
inner join posts p on p.forumid=f.id --u used in your query PostLeftOuterJoin.id ( is this the forum id or the posts id ?)
where p.status='approved' and p.object_type='forum_topic'
group by f.id,f.title,f.modified
union
select f.id,f.title,f.modified,Type='Replies',Number=count(*)
from forum_topics as f
inner join posts p on p.forumid=f.id --u used in your query PostLeftOuterJoin.id ( is this the forum id or the posts id ?)
where p.status='approved' and p.object_type='posts'
group by f.id,f.title,f.modified
to distinguish between the posts and replies, i added the Type
and from your application level you can get the forum post count when Type=Posts and same as for the replies
another method
select f.id,f.title,f.modified,Posts=(select count(*) from posts p where f.id=p.object_id and p.status='approved' and p.object_type='forum_topic'),
Replies=(select count(*) from posts p on where f.id=p.object_id
and p.status='approved' and p.object_type='posts')
from forum_topics f
where f.forum_category_id='someid'
hope that this will help you
regards

How to use subqueries in MySQL

I have two tables.
Table user:
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(20) NOT NULL AUTO_INCREMENT,
`ud_id` varchar(50) NOT NULL,
`name` text NOT NULL,
`password` text NOT NULL,
`email` varchar(200) NOT NULL,
`image` varchar(150) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB
and mycatch:
CREATE TABLE IF NOT EXISTS `mycatch` (
`catch_id` int(11) NOT NULL AUTO_INCREMENT,
`catch_name` text NOT NULL,
`catch_details` text NOT NULL,
`longitude` float(10,6) NOT NULL,
`latitude` float(10,6) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`image` varchar(150) NOT NULL,
`user_id` int(20) NOT NULL,
PRIMARY KEY (`catch_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB;
ALTER TABLE `mycatch`
ADD CONSTRAINT `mycatch_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
My goal is: I want to retrieve longitude and latitude from mycatch against given ud_id (from user) and catch_id (from mycatch) where ud_id = given ud_id and catch_id > given catch_id.
I used the query but fail to retrieve
SELECT ud_id=$ud_id FROM user
WHERE user_id =
(
SELECT user_id,longitude,latitude from mycatch
WHERE catch_id>'$catch_id'
)
The error is:
#1241 - Operand should contain 1 column(s)
First, try not to use subqueries at all, they're very slow in MySQL.
Second, a subquery wouldn't even help here. This is a regular join (no, Mr Singh, not an inner join):
SELECT ud_id FROM user, mycatch
WHERE catch_id>'$catch_id'
AND user.user_id = mycatch.user_id
Select m.longitude,m.latitude from user u left join mycatch m
on u.user_id =m.user_id
where u.ud_id=$ud_id and
m.catch_id >$catch_id

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

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'

Cant figure out how to join these tables

Need help with some kind of join here. Cant figure it out.
I want to loop out the forum boards, but I also want to get last_post and last_posterid from posts, and based on last_posterid get username from users.
This is how far I've come yet (:P):
SELECT name, desc, position FROM boards b
INNER JOIN posts p ON ???
INNER JOIN users u ON ???
ORDER BY b.position ASC
Help would be greatly appreciated.
Cheers
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`posterid` int(10) unsigned NOT NULL DEFAULT '0',
`subject` varchar(255) DEFAULT NULL,
`message` text,
`posted` int(10) unsigned NOT NULL DEFAULT '0',
`edited` int(10) unsigned DEFAULT NULL,
`edited_by` int(10) unsigned NOT NULL DEFAULT '0',
`icon` varchar(255) DEFAULT NULL,
`topicid` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
CREATE TABLE `boards` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`desc` varchar(255) NOT NULL,
`position` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
CREATE TABLE `users` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(200) NOT NULL,
`password` varchar(255) NOT NULL,
`salt` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
CREATE TABLE `topics` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`posterid` int(10) unsigned NOT NULL DEFAULT '0',
`subject` varchar(255) NOT NULL,
`posted` int(10) unsigned NOT NULL DEFAULT '0',
`last_post` int(10) unsigned NOT NULL DEFAULT '0',
`last_poster` int(10) unsigned NOT NULL DEFAULT '0',
`num_views` mediumint(8) unsigned NOT NULL DEFAULT '0',
`num_replies` mediumint(8) unsigned NOT NULL DEFAULT '0',
`closed` tinyint(1) NOT NULL DEFAULT '0',
`sticky` tinyint(1) NOT NULL DEFAULT '0',
`icon` varchar(40) DEFAULT NULL,
`boardid` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8
Assuming posts.topicid refers to boards.id, and posts.posterid refers to users.id, try something like
SELECT b.name, d.desc, b.position
FROM boards b
LEFT JOIN posts p
ON b.id = p.topicid
LEFT JOIN users u
ON (p.posterid = u.id) AND (p.posted = (SELECT MAX(sp.posted) FROM posts sp GROUP BY))
ORDER BY
b.position ASC
Another remark: try to name your fields such that it is clear what they refer to, for instance:
CREATE TABLE `foo` (
foo_ID UNSIGNED INTEGER NOT NULL AUTO_INCREMENT,
-- more stuff
PRIMARY KEY (`foo_ID`)
);
This allows you to re-use the field name foo_ID in another table, which makes things very easy when performing a join.
Nothing stands out, so this is mostly a guess:
SELECT b.name, b.desc, b.position
FROM boards AS b
INNER JOIN posts AS p
ON b.id = p.topicid
INNER JOIN users AS u
ON p.posterid = u.id
ORDER BY b.position ASC
SELECT username, message, posts.id
FROM users, posts
WHERE posterid=users.id
GROUP BY username, message, posts.id
HAVING posted=MAX(posted)
This might just do to get each user's last post concisely.
Ok, lack of a schema not withstanding, it looks like you could use nested select. Something like:
DECLARE #posts TABLE
(
postdate datetime,
postid int,
boardid int
)
DECLARE #users TABLE
(
userid int,
username varchar(10)
)
DECLARE #boards TABLE
(
boardid int
)
INSERT INTO #boards(boardid) VALUES (1)
INSERT INTO #users (userid, username) values (1, 'Adam')
insert into #posts (postdate, postid, boardid) values (GETDATE(), 1, 1)
SELECT b.*, p.*
from #boards b
inner join
(
SELECT TOP 1 postdate, postid, boardid, username FROM #posts
inner join #users on userid = postid WHERE boardid = 1
order by postdate desc
) as p on p.boardid = b.boardid
Oviously this is heavily generalised and uses some hard-coded values, but hopefully it should give you an option for implementing it on your own tables.
Of course, I infer no correctness in my implementation of the spec - SQL is not my first language lol
Was going to post this as a comment but it was too verbose and messy.
you'll want to use OUTER JOINs because otherwise boards with no posts in them will not show up
any single query that links those four tables is going to be a beast - consider using VIEWs to simplify things
your initial question and the tables you posted leave things a bit ambiguous: do you already have last_post and last_poster filled out in topics? If so, then your primary targets for the join should be boards and topics, and since you're already storing redundant data (I assume for speed?), why don't you also put in a last_post_time or something, which would simplify things a lot and curb load (no need to join in posts at all)?
mediumints? why on Earth? (If you don't need the date, and you think it'll never wrap, I suppose you can use the hack of trusting that a later post will have a higher ID due to autoincrementing key...)

sql query is returning the same values twice

I have this sql query, and it should be returning two values, which is does but it returns each returned row twice, the sql looks like this,
SELECT * FROM `mailers`
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailers`.`id` = 26
The table structure for the tables I am query look like this,
-- --------------------------------------------------------
--
-- Table structure for table `mailers`
--
CREATE TABLE `mailers` (
`id` int(11) NOT NULL auto_increment,
`mailer_title` varchar(150) NOT NULL,
`mailer_header` varchar(60) NOT NULL,
`mailer_type` enum('single','multi') NOT NULL,
`introduction` varchar(80) NOT NULL,
`status` enum('live','dead','draft') NOT NULL,
`flag` enum('sent','unsent') NOT NULL,
`date_mailer_created` int(11) NOT NULL,
`date_mailer_updated` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;
-- --------------------------------------------------------
--
-- Table structure for table `mailer_content`
--
CREATE TABLE `mailer_content` (
`id` int(11) NOT NULL auto_increment,
`headline` varchar(320) NOT NULL,
`content` text NOT NULL,
`mailer_id` int(11) NOT NULL,
`position` enum('left','right','centre') default NULL,
`tab_1_name` varchar(25) default NULL,
`tab_1_link` varchar(250) default NULL,
`tab_2_name` varchar(25) default NULL,
`tab_2_link` varchar(250) default NULL,
`tab_3_name` varchar(25) default NULL,
`tab_3_link` varchar(250) default NULL,
`tab_4_name` varchar(25) default NULL,
`tab_4_link` varchar(250) default NULL,
`created_at` int(10) NOT NULL,
`updated_at` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `mailer_id` (`mailer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
-- --------------------------------------------------------
--
-- Table structure for table `mailer_images`
--
CREATE TABLE `mailer_images` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(150) NOT NULL,
`filename` varchar(150) NOT NULL,
`mailer_id` int(11) NOT NULL,
`content_id` int(11) default NULL,
`date_created` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ;
I am sure that is must be a problem with my sql I just do not know what the problem is
If you use SELECT DISTINCT SQL will not return dupplicated rows, if there are some.
SELECT DISTINCT * FROM `mailers` LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id` LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id` WHERE `mailers`.`id` = 26
U can use group by smthng. It will delete the same records.
but u can delete nonsame rows. Use smthng without same values in different rows in original table.
try this
SELECT * FROM mailers
LEFT JOIN mailer_content ON mailers.id = mailer_content.mailer_id
LEFT JOIN mailer_images ON mailer_content.id = mailer_images.content_id
WHERE mailers.id = 26 GROUP BY mailers.id
It doesn't look like an SQL isse to me; I suspect this is more likely down to the data in your tables.
My guess is that there are two rows in mailer_content where mailers.id = 26 and then two rows (or possibly 1 and 3) in mailer_images for each of the mailer_contents.
How many rows do each of the following queries return?
SELECT * FROM `mailers`
WHERE `mailers`.`id` = 26
SELECT * FROM `mailer_content`
WHERE `mailer_content`.`id` = 26
My guess is that the first returns 1 row (because it has a primary key on id) and that the second returns two rows.
That all may be fine but my guess is that the following query returns 4 records:
SELECT * FROM `mailer_content`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailer_content`.`id` = 26
Because either each content has two images each OR one content has one image and the other has three.