-1

I am trying to create an SQL query that compares two databases with each other, however also will not result in a query failure if one of the database rows has not been created elsewhere yet.

Here's what I've gotten so far (a bit of pseudo code)

$Query="SELECT * FROM campaigns,statistics WHERE statistics.dataDate = '$todaysDate' OR statistics.dataDate DOES NOT EXIST AND where campaigns.campaignName = 'example'";

The "DOES NOT EXIST" part is where I need the help, how can I create a query that allows the query to continue with more checks if the row has not been created yet?

Thanks very much for any suggestions! I've taken a look around and can't find a solution that checks if the row doesn't exist inside of the query (will be needed for when the query is built out a bit further)

Thanks again,

Ash

edit:

To clear up any confusion here's a second example:

`$Query="SELECT * FROM campaigns,statistics WHERE campaigns.campaignName = '$randomVar' AND (campaigns.budget > statistics.budget OR statistics.budget DOES NOT EXIST )";`

I have created this example showing that if the budget does not exist (we would treat it as 0) and still return the results because the budget field hasn't been created yet elsewhere in the platform. If I run the query without this extra "OR" it will look something like this:

    `$Query="SELECT * FROM campaigns,statistics WHERE campaigns.campaignName = '$randomVar' AND (campaigns.budget > statistics.budget)";`

The problem with this is that in my platform so far the statistics.budget is not always created before this query is ran, therefor if it doesn't have a row I'm happy for the query to treat it as 0

Thanks!

Ash

ashlewis
  • 3
  • 3
  • Any record that *doesn't exist* simply won't be returned by the `SELECT` query. It's not really clear to me what you're asking here. Can you provide sample data to demonstrate? – David Sep 01 '16 at 18:34
  • That's my problem @David , I still want it to return the result if the statistics.dataDate does not exist in the database, instead of just failing the query I'd like to find a way of carrying on if the result hasn't been created yet. Thanks for the quick help! – ashlewis Sep 01 '16 at 18:35
  • 1
    So you want to return *all possible records which do not currently exist*? That would be an infinite amount of data. – David Sep 01 '16 at 18:36
  • I have edited my OP to make it a bit clearer, as mentioned at the end "will be needed for when the query is built out a bit further", so there will be other functions inside of this query, however I don't want the query to fail from the offset because of one piece of missing data in another database, I'm not sure if something for this problem exists but it would be super useful! – ashlewis Sep 01 '16 at 18:39
  • 2
    "*however also will not result in a query failure if one of the database rows has not been created elsewhere yet*"—Sounds like you're after an [outer join](https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/). – eggyal Sep 01 '16 at 18:40
  • The complete query is using outer joins correct! My problem is that I would like the query to continue if the outer join fails (I.e.) WHERE (campaign.name = "test" AND statistics.name = "test" OR statistics.name HASNT BEEN CREATED YET) (the hasn't been created yet is where I need the help!) – ashlewis Sep 01 '16 at 18:45
  • @ashlewis: What does "would like the query to continue" even mean? A query just returns a result of matching data. It's not an ongoing process. How is your outer join (that you're not showing us) "failing"? Maybe you're looking for `AND statistics.name IS NULL` ? – David Sep 01 '16 at 18:51
  • Hi @David thanks for the reply, IS NULL only returns rows that are null I'm afraid, I'm talking about a row that doesn't exist all together? Unless I have got the usage of IS NULL wrong, the reason the full query is not shown is because I'm trying to establish the theory, the full 30 or so rule query wouldn't help here, What I mean by "would like the query to continue" Is that I would still like it to return SOME data, instead of the entire query failing due to a single row that the query checks having not been created elsewhere in the platform (so isn't NULL?) – ashlewis Sep 01 '16 at 18:56
  • @ashlewis: If you could provide a complete and simplified example of what you're trying to do, that might make the question more clear. Currently none of this is really making any sense. A query will return the data which matches the conditions. If a row *doesn't exist* then it won't be returned by the query, nor will it affect the query in any way. Because it doesn't exist. Things have to exist in order to have an effect on other things. – David Sep 01 '16 at 18:59
  • hi @David, thanks for the reply again, I've posted another example in the OP, if you could take a look that would be awesome. – ashlewis Sep 01 '16 at 19:10

3 Answers3

0

What I understand from what you were asking is that you want to check if the Query return a results or not. If that's what you were asking this is the way to check if it is empty or if there is results were returned from the Query.

To check if the Query returns rows you can use Empty() function.

     $Query="SELECT * FROM campaigns,statistics WHERE statistics.dataDate = '$todaysDate' OR statistics.dataDate DOES NOT EXIST AND where campaigns.campaignName = 'example'";

 if(empty($Query))
    {
      // You can create any Query here , so when the $Query is empty and no data were returned Anything here is going to execute , you can create fields or anything you want . 
    }

  else 
    {
       // This is the will execute when there is returned data in the $Query , Its optional this .
   }
Blue
  • 22,608
  • 7
  • 62
  • 92
Laith
  • 428
  • 4
  • 10
  • Hi Laith, Thanks for your comment, I could have done this but unfortunately I was looking for a way to do it inside of the query, I could maybe use this and do a bit of a bodge job by creating a second query inside of the if/else - read the "inside of the query" in the OP, Thanks for the suggestion though! – ashlewis Sep 01 '16 at 18:50
  • Np , so you want to check if there is fields that exist in a database but not in another right ? – Laith Sep 01 '16 at 18:52
  • I'm not sure to be honest Laith, If you happen to know of a way to check if a row exists inside of the query (Like IS NULL but that only works for null results, not non-existent results)then by all means suggest it! It would be very much appreciated, Thanks very much :) – ashlewis Sep 01 '16 at 19:01
  • still trying to figure out how we can solve it , but check the new answer that someone just posted , and check the last part of his answer cause it important , the one talking about Query injection . @ashlewis – Laith Sep 01 '16 at 19:25
0

First your OR and AND code is muddled. You should definitely put some () to make it clean. Also you don't have a connection between your two databases so I made one up. Try This

$Query="SELECT * FROM campaigns LEFT OUTER JOIN statistics WHERE
(statistics.dataDate = '$todaysDate' OR statistics.dataDate IS NULL ) AND 
campaigns.campaignName = 'example' AND 
campaigns.campaignName=statistics.campaignName";
Forbs
  • 1,256
  • 1
  • 7
  • 9
  • Thanks for the suggestion I'll give this a try now, my first attempt with IS NULL failed, am I right in saying that IS NULL only returns positive when the row = null instead of the row not existing all together? (my problem is that the row in the other database doesn't exist, it isn't null) – ashlewis Sep 01 '16 at 18:58
  • That's why I put LEFT OUTER JOIN, without that there will never be a match will NULL. Now as long as there is a way to join them together, the LEFT outer join will allow NULLS – Forbs Sep 01 '16 at 19:01
  • Hi Forbs, thanks very much, I've posted another example really quickly, do you happen to know if this will still apply? I'm testing this method as we speak :) thanks again – ashlewis Sep 01 '16 at 19:09
  • Well 1) `DOES NOT EXIST` doesn't apply to a single query like that, you are using it wrong. 2) I don't see the `LEFT OUTER JOIN` so I don't know what happens 3) You don't have a connector between the two databases (No > isn't a good connector) so you will just get a X * Y result of rows returned – Forbs Sep 01 '16 at 19:29
0

Based on this example you provide:

SELECT *
FROM campaigns,statistics
WHERE
    campaigns.campaignName = '$randomVar'
    AND (campaigns.budget > statistics.budget
         OR statistics.budget DOES NOT EXIST)

First, explicitly define your JOIN:

SELECT *
FROM
    campaigns
    LEFT OUTER JOIN statistics
        ON campaigns.someColumn = statistics.someColumn
WHERE
    campaigns.campaignName = '$randomVar'
    AND (campaigns.budget > statistics.budget
         OR statistics.budget DOES NOT EXIST)

(Or whatever relational join makes sense for these tables.) Explicit joins are going to take a lot of the guesswork out of how your data is related.

If you want records where statistics.budget is NULL then you would do something like this:

SELECT *
FROM
    campaigns
    LEFT OUTER JOIN statistics
        ON campaigns.someColumn = statistics.someColumn
WHERE
    campaigns.campaignName = '$randomVar'
    AND (campaigns.budget > statistics.budget
         OR statistics.budget IS NULL)

Or if you want to further narrow it to only those records where campaigns.budget has a value at all, something like this:

SELECT *
FROM
    campaigns
    LEFT OUTER JOIN statistics
        ON campaigns.someColumn = statistics.someColumn
WHERE
    campaigns.campaignName = '$randomVar'
    AND (campaigns.budget > statistics.budget
         OR (statistics.budget IS NULL
             AND campaigns.budget IS NOT NULL))

When a value in a record "does not exist" that means the value is NULL. So you can use IS NULL and IS NOT NULL to check for the presence/absence of that case.


we would treat it as 0

That would take place in the SELECT clause. First, explicitly define the columns you want:

SELECT
    campaigns.aColumn,
    campaigns.anotherColumn,
    statistics.someOtherColumn

... and so on. Once you have that, you can define a clause to return a given column as a given value if it meets a certain condition. For example, you can return statistics.budget as-is if it isn't NULL, or 0 if it is. Something like this:

SELECT
    campaigns.aColumn,
    campaigns.anotherColumn,
    statistics.someOtherColumn,
    IFNULL(statistics.budget, 0) AS budget

Note: This is a SQL injection problem waiting to happen:

WHERE campaigns.campaignName = '$randomVar'

Use query parameters to treat values as values instead of inserting strings directly into the query as executable code.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • Perfect!! the IFNULL() Is awesome. Thanks for pointing it out. Really well created answer. Marked as answered. – ashlewis Sep 01 '16 at 19:27