MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 11th, 2012, 04:21 AM
JRowe95 JRowe95 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 JRowe95 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 19 sec
Reputation Power: 0
Help with a query

Below is the SQL code for a database.
I am having trouble doing a search query.
I need a query that will display the first name, last name and id of all the accounts that have tickets to the event with the id of `1`.
I know I need to use JOIN or something like that but I just cant understand how I can get it to work. Please help. Thanks in advance.

Code:
CREATE TABLE IF NOT EXISTS `accounts` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', `username` varchar(32) NOT NULL COMMENT 'Username', `password` char(128) NOT NULL COMMENT 'Password', `first_name` varchar(64) NOT NULL COMMENT 'First Name', `last_name` varchar(64) NOT NULL COMMENT 'Last Name', `dob` date NOT NULL COMMENT 'Date of Birth', `access` int(1) NOT NULL DEFAULT '0' COMMENT '0 = User, 1 = Admin', PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; INSERT INTO `accounts` (`id`, `username`, `password`, `first_name`, `last_name`, `dob`, `access`) VALUES (1, 'user1', 'b14361404c078ffd549c03db443c3fede2f3e534d73f78f77301ed97d4a436a9fd9db05ee8b325c0ad36438b43fec8510c2  04fc1c1edb21d0941c00e9e2c1ce2', 'John', 'Smith', '2012-11-11', 0); CREATE TABLE IF NOT EXISTS `acts` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(255) NOT NULL COMMENT 'Name', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; INSERT INTO `acts` (`id`, `name`) VALUES (1, 'Some Band'), (2, 'Some Other Band'); CREATE TABLE IF NOT EXISTS `events` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(255) NOT NULL COMMENT 'Name', `date` date NOT NULL COMMENT 'Date', `main_act` int(10) NOT NULL COMMENT 'Main Act', PRIMARY KEY (`id`), KEY `main_act` (`main_act`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; INSERT INTO `events` (`id`, `name`, `date`, `main_act`) VALUES (1, 'The Best Music Show', '2013-01-01', 1); CREATE TABLE IF NOT EXISTS `support_acts` ( `event_id` int(10) NOT NULL COMMENT 'Event ID', `act_id` int(10) NOT NULL COMMENT 'Act ID', KEY `event_id` (`event_id`,`act_id`), KEY `act_id` (`act_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `support_acts` (`event_id`, `act_id`) VALUES (1, 2); CREATE TABLE IF NOT EXISTS `tickets` ( `ticket_id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'Ticket ID', `account_id` int(10) NOT NULL COMMENT 'Account ID', `event_id` int(10) NOT NULL COMMENT 'Event ID', PRIMARY KEY (`ticket_id`), KEY `account_id` (`account_id`,`event_id`), KEY `event_id` (`event_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; INSERT INTO `tickets` (`ticket_id`, `account_id`, `event_id`) VALUES (1, 1, 1); ALTER TABLE `events` ADD CONSTRAINT `events_ibfk_1` FOREIGN KEY (`main_act`) REFERENCES `acts` (`id`) ON UPDATE CASCADE; ALTER TABLE `support_acts` ADD CONSTRAINT `support_acts_ibfk_1` FOREIGN KEY (`act_id`) REFERENCES `acts` (`id`) ON UPDATE CASCADE, ADD CONSTRAINT `support_acts_ibfk_2` FOREIGN KEY (`event_id`) REFERENCES `events` (`id`) ON UPDATE CASCADE; ALTER TABLE `tickets` ADD CONSTRAINT `tickets_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON UPDATE CASCADE, ADD CONSTRAINT `tickets_ibfk_2` FOREIGN KEY (`event_id`) REFERENCES `events` (`id`) ON UPDATE CASCADE;

Reply With Quote
  #2  
Old November 11th, 2012, 04:44 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 17 m 37 sec
Reputation Power: 4141
Quote:
Originally Posted by JRowe95
I know I need to use JOIN or something like that
correct, you do

where did you get that script to create the tables?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old November 24th, 2012, 02:43 AM
JRowe95 JRowe95 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 JRowe95 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by r937
correct, you do

where did you get that script to create the tables?


I made it myself in phpMyAdmin and just exported it as an SQL file.

Reply With Quote
  #4  
Old December 2nd, 2012, 05:21 AM
bobert123 bobert123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 55 bobert123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 40 m 30 sec
Reputation Power: 2
should be something like this:
Quote:
SELECT accounts.id, accounts.first_name, account.last_name from accounts JOIN tickets ON accounts.id=tickets.account_id WHERE tickets.event_id=1

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help with a query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap