kenny Posted January 2, 2016 at 02:03 PM Report Share #591198 Posted January 2, 2016 at 02:03 PM Boas tenho duas tabelas que a estrutura é exactamente a mesma e tenho uma query inicial que faz a verificação numa a query está aqui: FormatEx(Query, sizeof(Query), "SELECT bid FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, auth[8], ip) Isto é para um jogo e está no codigo do mesmo onde os %s são as variáveis que estão no fim. 1º -> %s = DatabasePrefix 2º -> %s = auth[8] (que é o ID da steam para quem conhece) 3º -> %s = ip do jogador E agora eu fiz um copy paste para construir uma segunda tabela com a mesma estrutura exactamente igual e queria fazer uma query que verifica se as duas tabelas e verificar se existe o jogador nas duas tabelas falaram me do "Inner Join" mas eu não compreendi muito bem como fazer lo e queria uma ajuda se possível para fazer isso. Segue abaixo a estrutura das duas tabelas que é a mesma. Agradeço desde já a ajuda porque eu nao sei mesmo por onde começar. Obrigado e Cumprimentos. Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 02:08 PM Report Share #591199 Posted January 2, 2016 at 02:08 PM Presumindo que o campo de igualdade será o authid em ambas as tabelas SELECT * FROM tabela_1 AS t1 INNER JOIN tabela_2 AS t2 ON t2.authid = t1.authid Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 02:48 PM Author Report Share #591202 Posted January 2, 2016 at 02:48 PM Desculpa bioshock Não percebi muito bem as tabelas chamam se "sb_bans" e "sb_bansdisconect" ou seja o tabela_1 = sb_bans e o tabela_2 = sb_bansdisconect e agora o AS t1 e AS t2 o que vai ser? Como ficará com o código que mandei consegues me mostrar? Obrigado pela atenção e desculpa nao ter percebido logo. Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 03:58 PM Report Share #591203 Posted January 2, 2016 at 03:58 PM AS é uma expressão SQL para definir, dali para a frente, uma expressão mais curta de determinada tabela/campo. Por exemplo, sb_bans AS bans significa que daqui em diante esta tabela será referida como bans apenas. SELECT * FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bansdisc.authid = bans.authid Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 05:04 PM Author Report Share #591204 Posted January 2, 2016 at 05:04 PM (edited) Ou seja ficaria assim entao : FormatEx(Query, sizeof(Query), "SELECT bid FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bansdisc.authid = bans.authid WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", auth[8], ip) Edited January 2, 2016 at 05:05 PM by kenny Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 05:12 PM Report Share #591205 Posted January 2, 2016 at 05:12 PM (edited) Não. Convém, na sequência do que já foi dito, referenciar qual a tabela onde estás a fazer as condições. ... WHERE ((bans.type = 0 AND bans.authid ...etc Edited January 2, 2016 at 05:12 PM by bioshock Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 05:19 PM Author Report Share #591206 Posted January 2, 2016 at 05:19 PM Assim: FormatEx(Query, sizeof(Query), "SELECT bid FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bansdisc.authid = bans.authid WHERE ((bans.type = 0 AND bans.authid REGEXP '^STEAM_[0-9]:%s$') OR (bans.type = 1 AND bans.ip = '%s')) AND (bans.length = '0' OR bans.ends > UNIX_TIMESTAMP()) AND bans.RemoveType IS NULL", auth[8], ip) Mas sendo assim depois nao terei que fazer WHERE para a tabela bansdisc? Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 05:29 PM Report Share #591209 Posted January 2, 2016 at 05:29 PM Correcto. Isso já só tu saberás dizer quais as condições que queres ver em cada tabela. Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 05:31 PM Author Report Share #591210 Posted January 2, 2016 at 05:31 PM Pois é as mesmas e como faço outro WHERE dentro do mesmo ? Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 05:34 PM Report Share #591211 Posted January 2, 2016 at 05:34 PM Não tens de fazer outro WHERE, tens de adicionar mais condições com a outra tabela. ... WHERE ((bans.type = 0 AND bansdisc.type = 0..etc Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 05:45 PM Author Report Share #591212 Posted January 2, 2016 at 05:45 PM Assim: FormatEx(Query, sizeof(Query), "SELECT bid FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bansdisc.authid = bans.authid WHERE ((bans.type = 0 AND bans.authid REGEXP '^STEAM_[0-9]:%s$' AND bansdisc.type = 0 AND bansdisc.authid REGEXP '^STEAM_[0-9]:%s$') OR (bans.type = 1 AND bans.ip = '%s' AND bansdisc.type = 1 AND bansdisc.ip = '%s')) AND (bans.length = '0' OR bans.ends > UNIX_TIMESTAMP() AND bansdisc.length = '0' OR bansdisc.ends > UNIX_TIMESTAMP()) AND bans.RemoveType IS NULL AND bansdisc.RemoveType IS NULL", auth[8], auth[8], ip, ip) E será que nao há problema de repetir as variáveis no fim ? Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 05:49 PM Report Share #591213 Posted January 2, 2016 at 05:49 PM Tens de as repetir se não é suficiente fazer o match de um campo só. Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 05:55 PM Author Report Share #591214 Posted January 2, 2016 at 05:55 PM Eu só perguntei mais como no inicio da query tem o "ON" (bansdisc.authid = bans.authid) para ver se dava para fazer mais "ON" e escusar de repetir os WHERE. Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 06:12 PM Report Share #591216 Posted January 2, 2016 at 06:12 PM Ahhh, não tinha percebido. Há diferenças do que estás a pensar para o que é possível. Tu estás a criar condições (WHERE) de valores específicos. A expressão ON tem como propósito fazer match entre campos. Podes especificar vários, por exemplo: SELECT * FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bansdisc.authid = bans.authid AND bansdisc.name = bans.name AND bansdisc.created = bans.created AND (bansdisc.type = bans.type OR bansdisc.sid = bans.sid); Link to comment Share on other sites More sharing options...
kenny Posted January 2, 2016 at 07:40 PM Author Report Share #591220 Posted January 2, 2016 at 07:40 PM Nao percebi muito bem o que é fazer match entre campos. Mas entao é melhor como eu postei e fazer tudo no "Where" ? Link to comment Share on other sites More sharing options...
bioshock Posted January 2, 2016 at 10:56 PM Report Share #591222 Posted January 2, 2016 at 10:56 PM O sentido de match que tentei passar é de igualdade, correspondência. ON bansdisc.authid = bans.authid Ou seja, o campo authid da tabela bansdisc obrigatoriamente tem de ser igual ao campo authid da tabela bans. As condições são necessárias quando precisas de atribuir certos valores, que é o que estás a fazer. WHERE bans.type = 0 Link to comment Share on other sites More sharing options...
kenny Posted January 3, 2016 at 12:44 AM Author Report Share #591228 Posted January 3, 2016 at 12:44 AM (edited) Fiz alguns testes com esta Query: SELECT * FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc WHERE ((bans.type = 0 AND bans.authid = 'STEAM_0:0:111274527' OR bansdisc.type = 0 AND bansdisc.authid = 'STEAM_0:0:111274527' ) OR (bans.type = 1 AND bans.ip = '%s' AND bansdisc.type = 1 AND bansdisc.ip = '%s')) AND (bans.length = '0' OR bans.ends > UNIX_TIMESTAMP() AND bansdisc.length = '0' OR bansdisc.ends > UNIX_TIMESTAMP()) AND bans.RemoveType IS NULL AND bansdisc.RemoveType IS NULL E retornou me o valor na base de dados bans até aqui correto. Mas se eu trocar o valor authid para um valor que tenha na base de dados bansdisc ele nao me retorna nenhum valor e diz que nao tem nada. SELECT * FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc WHERE ((bans.type = 0 AND bans.authid = 'STEAM_0:0:14218688' OR bansdisc.type = 0 AND bansdisc.authid = 'STEAM_0:0:14218688' ) OR (bans.type = 1 AND bans.ip = '%s' AND bansdisc.type = 1 AND bansdisc.ip = '%s')) AND (bans.length = '0' OR bans.ends > UNIX_TIMESTAMP() AND bansdisc.length = '0' OR bansdisc.ends > UNIX_TIMESTAMP()) AND bans.RemoveType IS NULL AND bansdisc.RemoveType IS NULL Tenho esta authid na base dados bansdisc e diz me que nao tem nenhum valor. EDIT: Eu nao sei se me expliquei bem bioshock eu nao sei se é possivel eu quero verificar uma tabela e se esse valor nao tiver na tabela verificar noutra isso é com o INNER JOIN ? Edited January 3, 2016 at 03:33 AM by kenny Link to comment Share on other sites More sharing options...
bioshock Posted January 3, 2016 at 11:52 AM Report Share #591238 Posted January 3, 2016 at 11:52 AM (edited) A tua query está mal, não estás a dar uso nenhum ao INNER JOIN. Eu disse-te como devias utilizar o INNER JOIN no segundo post. Fazes sequer ideia para que serve o INNER JOIN? É que estares a fazer código por fazer, sem o perceberes, não vale a pena. Edited January 3, 2016 at 11:52 AM by bioshock Link to comment Share on other sites More sharing options...
kenny Posted January 3, 2016 at 12:37 PM Author Report Share #591239 Posted January 3, 2016 at 12:37 PM (edited) Com o "ON" não funciona nada. Eu so estou a usar o INNER JOIN porque me disseram eu nao sei ao certo se é isso que tenho que usar. Por isso estou a perguntar e com duvidas para ver se é o INNER JOIN ou outra coisa qualquer. Desculpa eu não estudo código o que aprendi foi a ver na net. 😞 :( Se eu utilizar assim: SELECT * FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bans.authid = bansdisc.authid WHERE ((bans.type = 0 AND bans.authid = 'STEAM_0:0:111274527' OR bansdisc.type = 0 AND bansdisc.authid = 'STEAM_0:0:111274527' ) OR (bans.type = 1 AND bans.ip = '%s' AND bansdisc.type = 1 AND bansdisc.ip = '%s')) AND (bans.length = '0' OR bans.ends > UNIX_TIMESTAMP() AND bansdisc.length = '0' OR bansdisc.ends > UNIX_TIMESTAMP()) AND bans.RemoveType IS NULL AND bansdisc.RemoveType IS NULL Não me retorna nenhum valor e eu tenho esse valor na base de dados "bans" e se tirar o "ON" já me retorna o valor, Edited January 3, 2016 at 12:42 PM by kenny Link to comment Share on other sites More sharing options...
bioshock Posted January 3, 2016 at 01:39 PM Report Share #591241 Posted January 3, 2016 at 01:39 PM (edited) Se te disserem para assaltar um banco, também vais fazê-lo? 😄 Antes de continuares deves então perceber a utilidade do INNER JOIN, até porque existem outros JOINs. http://www.w3schools.com/sql/sql_join_inner.asp http://pt.stackoverflow.com/questions/6441/qual-%C3%A9-a-diferen%C3%A7a-entre-inner-join-e-outer-join Não te retorna nenhum valor porque tens muitas condições aí para o meio, que forçam diferentes resultados. Se fizeres uma query simples irá te retornar - desde que haja correspondência de valores numa tabela e na outra. SELECT * FROM sb_bans AS bans INNER JOIN sb_bansdisconect AS bansdisc ON bansdisc.authid = bans.authid Tirando a expressão ON é a mesma coisa que fazeres (e que talvez te sirva para o problema): SELECT * FROM sb_bans AS bans, sb_bansdisconect AS bansdisc WHERE (....) Edited January 3, 2016 at 01:39 PM by bioshock Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now