We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to insert this subqueries into mysql?

Hi,guys,i cant solve this insert query into ,this is my query,i want it to insert it to another new table.How to solve this query?thank Q very for the help...cheers

SELECT adp.adsPageId,adp.adsSpaceId, DATE_FORMAT(adp.dayClick, '%Y-%m-%d') AS dateReport, 
(SELECT COUNT(*) FROM ads_display att WHERE att.adsSpaceId = adp.adsSpaceId AND att.adsPageId = adp.adsPageId AND att.dayClick = adp.dayClick) as TotalView,
(SELECT COUNT(*) FROM ads_click acc WHERE acc.adsSpaceId = ack.adsSpaceId AND acc.adsPageId = ack.adsPageId AND acc.dayClick = ack.dayClick) as UniqueClick

FROM ads_display adp

LEFT JOIN ads_click ack

ON adp.adsSpaceId = ack.adsSpaceId

AND adp.adsPageId = ack.adsPageId

AND adp.dayClick = ack.dayClick 

GROUP BY dateReport,adp.adsPageId,adp.adsSpaceId

ORDER BY dateReport,adsPageId,adsSpaceId ASC

New table column will be like this

(reportId,adsPageId,adsSpaceId,dateReport,totalView,totalClick)

Note: the reportId is the auto-increment id

3
Contributors
3
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
stanley87
Light Poster
43 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

Use the INTO statement after your SQL SELECT statement (..as UniqueClick..) before the FROM elememt.

So .. INTO tableName

tableName will then be created on the fly if not already created, else updated.

http://www.w3schools.com/sql/sql_select_into.asp

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
Skill Endorsements: 0

Hello,

It is really very simple but it took me a while to find it too.

create table newtable
SELECT adp.adsPageId,adp.adsSpaceId, DATE_FORMAT(adp.dayClick, '%Y-%m-%d') AS dateReport, 
(SELECT COUNT(*) FROM ads_display att WHERE att.adsSpaceId = adp.adsSpaceId AND att.adsPageId = adp.adsPageId AND att.dayClick = adp.dayClick) as TotalView,
(SELECT COUNT(*) FROM ads_click acc WHERE acc.adsSpaceId = ack.adsSpaceId AND acc.adsPageId = ack.adsPageId AND acc.dayClick = ack.dayClick) as UniqueClick

FROM ads_display adp

LEFT JOIN ads_click ack

ON adp.adsSpaceId = ack.adsSpaceId

AND adp.adsPageId = ack.adsPageId

AND adp.dayClick = ack.dayClick 

GROUP BY dateReport,adp.adsPageId,adp.adsSpaceId

ORDER BY dateReport,adsPageId,adsSpaceId ASC
rch1231
Veteran Poster
1,040 posts since Sep 2009
Reputation Points: 142
Solved Threads: 154
Skill Endorsements: 12

thanks for your guys reply,i solve this myself using code below,no doubt it is simple only ==,

INSERT INTO summaryReport(adsPageId,adsSpaceId,dateReport,totalView,totalClick)

SELECT adp.adsPageId,adp.adsSpaceId, DATE_FORMAT(adp.dayClick, '%Y-%m-%d') AS dateReport, 
(SELECT COUNT(*) FROM ads_display att WHERE att.adsSpaceId = adp.adsSpaceId AND att.adsPageId = adp.adsPageId AND att.dayClick = adp.dayClick) as TotalView,
(SELECT COUNT(*) FROM ads_click acc WHERE acc.adsSpaceId = ack.adsSpaceId AND acc.adsPageId = ack.adsPageId AND acc.dayClick = ack.dayClick) as UniqueClick
FROM ads_display adp
LEFT JOIN ads_click ack
ON adp.adsSpaceId = ack.adsSpaceId
AND adp.adsPageId = ack.adsPageId
AND adp.dayClick = ack.dayClick 
GROUP BY dateReport,adp.adsPageId,adp.adsSpaceId
ORDER BY dateReport,adsPageId,adsSpaceId ASC;
stanley87
Light Poster
43 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by StephNicolaou and rch1231

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0677 seconds using 2.69MB