The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
How to write a SQL request less?
Discuss How to write a SQL request less? in the MySQL Help forum on Dev Shed. How to write a SQL request less? MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 22nd, 2012, 01:26 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
|
|
How to write a SQL request less?
Hello from Russia! I have a code like this:
sql Code:
Original
- sql Code |
|
|
|
SELECT `cabinet`.`name_test`, `answers`.`answer_id`, ( SELECT `answer_id` FROM `answers` WHERE `user_id` = '353281' AND `answers`.`test_id` = `cabinet`.`test_id` ) AS answer_2 FROM `cabinet` INNER JOIN `answers` 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`?
|

December 22nd, 2012, 04:14 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
|
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?
|

December 23rd, 2012, 03:15 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
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?
|

December 23rd, 2012, 04:37 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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"
|

December 23rd, 2012, 04:58 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
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.
|

December 23rd, 2012, 06:24 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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' )
|

December 23rd, 2012, 06:57 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 4 h 53 m 53 sec
Reputation Power: 0
|
|
sql Code:
Original
- sql Code |
|
|
|
SELECT `cabinet`.`name_test`, `answers`.`answer_id`, `answers`.`answer_id` AS answer2 FROM `cabinet` INNER JOIN `answers` ON `answers`.`test_id` = `cabinet`.`test_id` 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.
|

December 23rd, 2012, 08:19 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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
|

December 23rd, 2012, 08:50 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
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`.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|