简体   繁体   English

带有 PHP 的 SQLite3 如何使用在其他列中找到的数据对列进行交叉引用

[英]SQLite3 with PHP How to do Cross Referencing of Columns with data found in other Columns

Here is how my Sample data looks like这是我的示例数据的样子

hwid: 1502e3c3-b49c-4907-92fd-2ffac2d50ace
ip: 68.197.140.109 
character name: none


hwid: 1502e3c3-b49c-4907-92fd-2ffac2d50ace
ip: 219.100.37.236
character name: none


hwid: 1502e3c3-b49c-4907-92fd-2ffac2d50acd 
ip: 68.197.140.109 
character name: kalgame

Now say I'm logging in with this hwid 1502e3c3-b49c-4907-92fd-2ffac2d50ace and this ip address 219.100.37.236现在说我正在使用这个 hwid 1502e3c3-b49c-4907-92fd-2ffac2d50ace 和这个 ip 地址 219.100.37.236 登录

which has a link to using a previous IP address of 68.197.140.109它有一个链接到使用以前的IP地址68.197.140.109
and that IP address has a link to a 3rd hwid that has a character name: kalgame .并且该IP地址具有指向具有character name: kalgame的第三个hwid的链接: kalgame 。

Thats what I need to do cross referencing with all IP's linked to all hwid's and then check if any of those hwid's have a character_name .这就是我需要对链接到所有hwid's所有IP's进行交叉引用,然后检查这些hwid's中是否有任何一个具有character_name If they have a character_name associated to them return them in a SELECT DISTINCT COUNT(totalMatchesOfUniqueCharacterNames)如果他们有关联的character_name ,则将它们返回SELECT DISTINCT COUNT(totalMatchesOfUniqueCharacterNames)

I also need to check if the current HWID or current IP you are using has 3 days trial period, if it does then also add it to the SELECT DISTINCT COUNT .. then I could easily check in PHP if SELECT DISTINCT COUNT is greater then 1, then abort giving out any more free trials to the specific user as he already logged in before with a character_name , and also maybe had a 3 day trial period before hand on either his PC hwid or his IP address, note I would also like to exclude blank character_name's as those players didn't login with a character yet.. so they don't count. I also need to check if the current HWID or current IP you are using has 3 days trial period, if it does then also add it to the SELECT DISTINCT COUNT .. then I could easily check in PHP if SELECT DISTINCT COUNT is greater then 1 ,然后中止向特定用户提供更多免费试用,因为他之前已经使用character_name登录,并且可能在他的 PC hwid或他的IP地址之前有 3 天的试用期,注意我也想排除空白character_name's ,因为那些玩家还没有使用角色登录..所以他们不计算在内。 I also have to match the locale which is which game are they playing if they are playing a different game then they also don't count.我还必须匹配他们正在玩的游戏的locale ,如果他们玩的是不同的游戏,那么他们也不算数。

Here is my attempt at cross referencing I been working at this for 6 hours and today is my first day using SQLite or any SQL in general so I have no clue what i'm doing这是我尝试交叉引用的尝试,我已经为此工作了 6 个小时,今天是我使用 SQLite 或任何 SQL 的第一天,所以我不知道我在做什么

Here is my table schema DDL for both tables.. they are also both in the same database file.这是两个表的表模式 DDL。它们也都在同一个数据库文件中。

CREATE TABLE ips (
    hwid           VARCHAR,
    ip             VARCHAR,
    character_name VARCHAR,
    locale         VARCHAR,
    botver         VARCHAR,
    reg_time       DATETIME,
    last_time      DATETIME,
    ping_count     INTEGER  DEFAULT 0
);

CREATE TABLE users (
    hwid      VARCHAR UNIQUE,
    timestamp INTEGER,
    daysToUse INTEGER,
    pcLimit   INTEGER,
    comment   VARCHAR
);

Here is my attempt at putting it all together这是我尝试将它们放在一起的尝试

SELECT users2.hwid,
       ips2.character_name,
       ips2.ip
  FROM users AS users2
       INNER JOIN
       ips AS ips2 ON ips2.hwid = users2.hwid
 WHERE EXISTS (
           SELECT 1
             FROM users
                  INNER JOIN
                  ips ON ips.hwid = users.hwid
            WHERE ips.character_name != '' AND 
                  ips.locale = ips2.locale AND 
                  (ips.hwid = '1502e3c3-b49c-4907-92fd-2ffac2d50ace' OR 
                   ips.ip = '219.100.37.236') AND 
                  EXISTS (
                          SELECT 1
                            FROM users
                                 INNER JOIN
                                 ips ON ips.hwid = users.hwid
                           WHERE ips.character_name != '' AND 
                                 ips2.ip = ips.ip AND 
                                 ips2.locale = ips.locale
                      )
       )
OR 
       (ips2.hwid = '1502e3c3-b49c-4907-92fd-2ffac2d50ace' AND 
        users2.daysToUse = 3) OR 
       (ips2.ip = '219.100.37.236' AND 
        users2.daysToUse = 3);

Here is what I left in the production website.. which isn't that good.. doesn't do cross referencing that well skips certain columns but atleast it kinda works unlike the top code above which just sounds like it should work but doesn't work at all.这是我在生产网站上留下的内容.. 这不是很好.. 交叉引用不能很好地跳过某些列,但至少它有点像上面的顶部代码那样工作,听起来它应该工作但没有根本不工作。

SELECT COUNT(DISTINCT users2.hwid) 
  FROM users AS users2
       INNER JOIN
       ips AS ips3 ON ips3.hwid = users2.hwid
 WHERE ips3.character_name = (
                                 SELECT ips.character_name
                                   FROM users,
                                        ips AS ips2
                                        INNER JOIN
                                        ips ON ips.hwid = users.hwid
                                  WHERE ips.character_name != '' AND 
                                        ips.locale = ips2.locale AND 
                                        (ips.hwid = '$HWID' OR 
                                         ips.ip = '$IP') 
                             )
OR 
       (ips3.hwid = '$HWID' AND 
        users2.daysToUse = 3) OR 
       (ips3.ip = '$IP' AND 
        users2.daysToUse = 3);

If anyone can help me out i will be forever grateful:D如果有人可以帮助我,我将永远感激:D

Since your subqueries share same FROM and JOIN clauses, simplify to a compact WHERE clause:由于您的子查询共享相同的FROMJOIN子句,因此简化为紧凑的WHERE子句:

SELECT i.ip, i.character_name, u.hwid, u.daysToUse, i.locale 
FROM users u
INNER JOIN ips i
   ON i.hwid = u.hwid 
WHERE (i.hwid = '1502e3c3-b49c-4907-92fd-2ffac2d50ace'
        AND u.daysToUse = 3) 
   OR (i.ip = '219.100.37.236' AND u.daysToUse = 3) 
ORDER BY i.last_time 

Solved it myself.. turns out it is very simple to do this, I just had the query done in backwards order I flipped the order and bam it works as expected.自己解决了..事实证明这样做很简单,我只是按倒序完成了查询,然后我翻转了顺序并按预期工作。

The locale problem was hard.. I couldn't detect which locale is currently used, so I relayed on using ips.lasttime which shows which IP or HWID was used last, and being used last its obviously the locale I want to check语言环境问题很难..我无法检测到当前使用的是哪个语言环境,所以我继续使用ips.lasttime它显示最后使用了哪个IPHWID ,并且最后使用它显然是我想要检查的语言环境

Final result最后结果

SELECT ips.ip,
       ips.character_name,
       users.hwid,
       users.daysToUse,
       ips.locale
  FROM users
       INNER JOIN
       ips ON ips.hwid = users.hwid
 WHERE ips.ip IN (
           SELECT ips.ip
             FROM users
                  INNER JOIN
                  ips ON ips.hwid = users.hwid
            WHERE (ips.hwid = '1502e3c3-b49c-4907-92fd-2ffac2d50ace' AND 
                   users.daysToUse = 3) OR 
                  (ips.ip = '219.100.37.236' AND 
                   users.daysToUse = 3) 
       )
AND 
       ips.locale = (
           SELECT ips.locale
             FROM users
                  INNER JOIN
                  ips ON ips.hwid = users.hwid
            WHERE (ips.hwid = '1502e3c3-b49c-4907-92fd-2ffac2d50ace' AND 
                   users.daysToUse = 3) OR 
                  (ips.ip = '219.100.37.236' AND 
                   users.daysToUse = 3) ORDER BY ips.last_time ASC
       );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM