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 December 22nd, 2012, 01:26 PM
Dimaz Dimaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 Dimaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
Question How to write a SQL request less?

Hello from Russia! I have a code like this:
sql Code:
Original - sql Code
  1. SELECT `cabinet`.`name_test`, `answers`.`answer_id`, (
  2. SELECT `answer_id` FROM `answers` WHERE `user_id` = '353281' AND `answers`.`test_id` = `cabinet`.`test_id`
  3. ) AS answer_2 FROM `cabinet` INNER JOIN `answers`
  4.  WHERE `user_id` = '184918649' AND `friend_id` = '353281' AND `answers`.`test_id` = `cabinet`.`test_id`

How not to write twice: `answers`.`test_id` = `cabinet`.`test_id`?

Reply With Quote
  #2  
Old December 22nd, 2012, 04:14 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,355 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 4 h 25 m
Reputation Power: 4140
could you please do a SHOW CREATE TABLE for each table

also, in your own words, please, what is the query supposed to do or trying to do?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 23rd, 2012, 03:15 AM
Dimaz Dimaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 Dimaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
Select from the table, responses of the first and second users. If the other user has not responded to one of the tests, in this case is written NULL.
Is it possible to write `answers`.`test_id` = `cabinet`.`test_id` once? My code queries the table twice. Is it possible to query a table only once?

Reply With Quote
  #4  
Old December 23rd, 2012, 04:37 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,355 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 4 h 25 m
Reputation Power: 4140
Quote:
Originally Posted by Dimaz
Is it possible to query a table only once?
sure, but that depends on what you're trying to accomplish

i don't understand "first and second users"

Reply With Quote
  #5  
Old December 23rd, 2012, 04:58 AM
Dimaz Dimaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 Dimaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
For `user_id` = '184918649' and `user_id` = '353281', get a list of response IDs. Tests are the same.

Reply With Quote
  #6  
Old December 23rd, 2012, 06:24 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,355 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 4 h 25 m
Reputation Power: 4140
Quote:
Originally Posted by Dimaz
For `user_id` = '184918649' and `user_id` = '353281', get a list of response IDs.

Code:
SELECT answer_id 
  FROM answers 
 WHERE user_id IN ( '184918649' , '353281' )

Reply With Quote
  #7  
Old December 23rd, 2012, 06:57 AM
Dimaz Dimaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 Dimaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
sql Code:
Original - sql Code
  1. SELECT `cabinet`.`name_test`, `answers`.`answer_id`, `answers`.`answer_id` AS answer2
  2.  FROM `cabinet` INNER JOIN `answers` ON `answers`.`test_id` = `cabinet`.`test_id`
  3.  WHERE `user_id` IN('184918649' AND `friend_id` = '353281', '353281')

name_test answer_id answer2
Test1_______111_______111
Test2_______222_______222
I need this:
name_test answer_id answer2
Test1_______111_______NULL
Test2_______222_______211
Сondition for user1: `user_id` = '184918649' AND `friend_id` = '353281'
Сondition for user2: `user_id` = '353281'. friend_id need not.

Reply With Quote
  #8  
Old December 23rd, 2012, 08:19 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,355 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 4 h 25 m
Reputation Power: 4140
Quote:
Originally Posted by Dimaz
Code:
 WHERE `user_id` IN('184918649' AND `friend_id` = '353281', '353281')

that's clearly invalid sql

and you still need to explain what you're actually trying to do

Reply With Quote
  #9  
Old December 23rd, 2012, 08:50 AM
Dimaz Dimaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 Dimaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
See my first post. I want to do the same, but not to write twice `answers`.`test_id` = `cabinet`.`test_id`.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > How to write a SQL request less?

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