1
\$\begingroup\$

I was able to pull the records I wanted but I have a feeling it could be written better for these particular MySQL query strings.

SELECT ID,XAxis,YAxis,Player,Castle,Alliance FROM SC75
WHERE Snap = (SELECT MAX(SavedSnap) FROM DataSnap WHERE Server = 75)
AND hex(Player) IN (hex('Vandiel'))
UNION
SELECT SC.ID,XAxis,YAxis,SC.Player,Castle,Alliance FROM SC75 AS SC
INNER JOIN AltFiller AS AF ON hex(AF.Player) IN (hex('Vandiel'))
WHERE SC.Player = AF.Alts
AND Snap = (SELECT MAX(SavedSnap) FROM DataSnap WHERE Server = 75)
ORDER BY Player,Castle
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jun 28, 2013 at 18:19
\$\endgroup\$
6
  • \$\begingroup\$ This doesn't look too awkward given the scarce information provided, but for starters - why do you have a table named SC75? Does that mean that you also have tables SC74, SC73 and so forth? Does the 75 in SC75 relate to Server = 75? If you posted the schema, I could have some comments. \$\endgroup\$ Commented Jun 28, 2013 at 20:24
  • \$\begingroup\$ Pretty much and I came across other Database knowledgable person and was pointed out a few flaw but improved, will be posting in a few what was pointed out :) \$\endgroup\$ Commented Jun 28, 2013 at 20:59
  • \$\begingroup\$ Okay, but a broken schema leads to broken queries. This query could potentially be much simpler if you used a better schema. In other words, asking about whether a query "looks right" will usually wind up becoming a discussion about the schema it is using. So you should start out getting the schema right (feel free to post it in another review question here). \$\endgroup\$ Commented Jun 28, 2013 at 21:02
  • \$\begingroup\$ See below for changes I've made. \$\endgroup\$ Commented Jun 28, 2013 at 21:10
  • \$\begingroup\$ Sorry. I think you're answering a question I didn't ask :) When i write "database schema", I refer to the table layout. \$\endgroup\$ Commented Jun 29, 2013 at 16:57

1 Answer 1

2
\$\begingroup\$

The two parts to the UNION have too much in common. I believe this query is equivalent to yours.

SELECT ID, XAxis, YAxis, Player, Castle, Alliance
 FROM SC75
 WHERE
 Snap = (SELECT MAX(SavedSnap) FROM DataSnap WHERE Server = 75)
 AND
 (
 (hex(Player) IN (hex('Vandiel'))
 OR
 Player IN
 (
 SELECT AF.Alts
 FROM AltFiller AS AF
 WHERE hex(AF.Player) IN (hex('Vandiel'))
 )
 )
 ORDER BY Player, Castle;
answered Aug 28, 2013 at 7:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.