How to solve this sql query? - php

I have the following query to fetch rows from wpr_subscribers provided that the conditions in the query.
SELECT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions subscriptions WHERE
subscribers.id=subscriptions.sid AND
subscribers.active=1 AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
The query returns two of every row. How do I solve this issue?

SELECT DISTINCT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions
subscriptions WHERE
subscribers.id=subscriptions.sid AND
subscribers.active=1 AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
DISTINCT keyword would help you in returning only the unique(distinct) rows in the table. Are you sure on the logic of AND and OR in the query and the unique data that you will now receive is indeed correct. Just for the info, AND operator has precedence over OR however you can override that by using parenthesis.

It is probably your OR statement
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
You should enclose this in parenthesis for whatever the scope of the OR is

Related

Join query codeigniter with where condition returns all rows

Here is my join query for getting data from two tables.
$this->db->select('*');
$this->db->from('george_hotel_bkd_customers');
$this->db->where('george_hotel_bookings.bookingref',$ref);
$this->db->join('george_hotel_bookings', 'george_hotel_bookings.user_id = george_hotel_bkd_customers.user_id');
$query = $this->db->get();
According to my where condition it returns only one row but it will returns all the rows with matches the join condition.
Seems like my where condition is not executed here.
please help me
What does $ref =, how many results should it pick up roughly?
what do you get when you omit the where? if you get full result then must be an issue with $ref value, or, that is the result.
Just a note, you don't need to select(*), this is default, if this is an just for examples sake, sorry for picking up. You can also add the ->from(whaterver_table) to ->get(whatever_table) You need to add ->get() anyway, so why not remove the ->from() line, just a choice of readability, but i did not realise you could do this for a while, so thought id add it to my answer.
Or another problem solving path is whether the join should be 'left' as 3rd arg. IE is there always a join?
Altering the position or the join in the statement would not make any difference, as Anant suggested
$this->db->select('*');
$this->db->from('george_hotel_bkd_customers');
$this->db->join('george_hotel_bookings', 'george_hotel_bookings.user_id = george_hotel_bkd_customers.user_id');
$this->db->where('george_hotel_bookings.bookingref',$ref);
$query = $this->db->get();
always where is last

SQL query with 2 AND possible?

I have the following query in SQL
$querycountry = mysql_query("SELECT
`country_iso2`
FROM
`ban_country_ip`
WHERE `ip_from` <= INET_ATON('$ip')
AND ip_to >= INET_ATON('$ip')
AND checkbox = 1
ORDER BY NAME ");
Is it a valid query if i added the second AND.
Any suggestions on how to overcome this.
Is it a valid query if i added the second AND.
Yes it is. You can use multiple conditions for a single query and AND or OR is used to add more than one conditions.
From http://www.tutorialspoint.com/mysql/mysql-where-clause.htm
You can specify more than one conditions using AND or OR operators.

PostgreSQL multiple criteria statement

I have the following postgreSQL statement that returns dates that school is open and attendance needs to be taken
SELECT school_date
FROM attendance_calendar
WHERE syear='$syear'
AND school_date<CURRENT_DATE
AND calendar_id='$cal_ID'
I then have another table called attendance_completed which has a column called school_date that i want to query for each returned date from the above statement. But instead of doing two different statements, just wondering if someone can help combine the statement?
Basically, i would like the above statement to check if the returned school_date exists in the table called attendance_completed, and only return the dates that are missing.
Thanks
This should work for you:
SELECT cal.school_date
FROM attendance_calendar cal
WHERE cal.school_date NOT IN (
SELECT com.school_date
FROM attendance_completed com
WHERE com.school_date IS NOT NULL
) AND
cal.syear = '$syear' AND
cal.school_date < CURRENT_DATE AND
cal.calendar_id = '$cal_ID;

MySQL SELECT COUNT with if statement - What is the best practice?

I'm getting inconsistent results with the way I'm doing this so I thought I'd ask the geniuses here what you think. Here is what I'm doing...
$ccquerycount = "SELECT COUNT(*) as `count` FROM payments_cc_info WHERE user_id=$userid";
....
....
if ($row['count'] == "1") {
} else {
}
Is there a better way to design the if statement to achieve consistent results?
I am assuming that you are executing the query, retrieving the result set, fetching the row and extracting the value before the if statement (if not, then perhaps you need to do that first! Here is a start for PHP-MySql.
In case if you have already done that and forgot to mention this in your post (innocent until proven guilty I always say!! :) ), then could you provide us with more data on what are the different datasets when you got inconsistent results?
Try this
$ccquerycount = "SELECT * FROM payments_cc_info WHERE user_id=$userid";
$result = mysql_query($ccquerycount)
if (mysql_num_rows($result)==1) {
} else {
}
mysql_num_rows()
Get number of rows in result
Warning mysql_ extension is being deprecated . Instead, the MySQLi or PDO_MySQL extension should be used .
You can also add if condition inside of your mysql query like below.
SELECT
rating,
trade_service_id,
ifnull(round(sum(belonging_product_ratings.rating)/count(*),0),0) AS avg,
count(*) AS count
FROM
belonging_product_ratings WHERE trade_service_id != 0
GROUP BY
belonging_product_ratings.trade_service_id
other alternative is as below
select
BelongingProductRating.*,
User.*
from
belonging_product_ratings AS BelongingProductRating
LEFT OUTER JOIN
users AS User ON
IF( BelongingProductRating.rated_user_id != '$userID', BelongingProductRating.rated_user_id, BelongingProductRating.user_id) = User.id
You can say if condition inside mysql query is same as ternary if.
You can set query according to your php variable too.
I am sharing this code which i was ended up.
all you need to do is set it according to your table fields and conditions.
feel free to ask.

searching between dates in MYSQL in this format 03/17/10.11:22:45

I have a script that automatically populates a mysql database with data every hour. It populates the date field like 03/17/10.12:34:11 and so on.
I'm working on pulling data based on 1 day at a time from a search script.
If i use
SELECT *
FROM call_logs
WHERE call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
it works, but when I try to add the rest of the search params, it ignores the call_initiated field.
SELECT *
FROM call_logs
WHERE caller_dn = '2x9xxx0000' OR called_dn = '2x9xxx0000'
AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
^-- I x'd out a couple of the numbers. I've also tried without the between function, and used >= <= to pull the records, but have the same results. Im sure its an oversight, thanks in advance.
try using parentheses around your OR statement
... where (caller_dn='2x9xxx0000' OR called_dn='2x9xxx0000') AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
use the IN operator instead of OR
... where caller_dn IN('2x9xxx0000','2y9yyyy000') AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
The OR statement is more than likely the issue, since OR evaluates the left side, sees that its true, it doesn't care what is on the right site, because as long as one of the statements is true, it considiers the entire statement to be true.
Read up on the OR operator at the MYSQL page
Your Statement should look like
SELECT * FROM `call_logs` WHERE (caller_dn='2x9xxx0000' OR called_dn='2x9xxx0000') AND call_initiated BETWEEN '03/17/10.12:00:00' and '03/17/10.13:00:00';

Categories