codeigniter active record db query - php

I need a query that looks like:
select * from table where id=5 and (eid = 1 or eid = 2 or eid = 10)
and I have tried
$this->db->select()->from(MEMB_EVENTS);
$this->db->where('mID', $mID);
with various means of where or_where get_where and no luck forming a query similar to the above need was hoping someone could shed some light for me on the subject

I think You can do that by using something like
$this->db->select()->from(MEMB_EVENTS);
$this->db->where('mID', $mID);
$this->db->where("(eid='1' OR eid='2' OR eid='10')", NULL, FALSE);

Related

Selecting data from 2 tables where columns are the same, query not quite right?

I'm trying to fetch data from 2 tables where the prop_slug and prop_gallery_id match. I've written the following statement only I cant seem to get it to work and keep getting an error with my syntax - can anybody see if there's a glaring mistake in my query?...
$listings = $db->query('
SELECT *
FROM listing_details
JOIN prop_gallery
ON prop_gallery.prop_gallery_id = listing_details.prop_slug
WHERE (listing_details.prop_slug LIKE \'prop_gallery_id\' OR prop_gallery.prop_gallery_id LIKE \'prop_slug\')
AND
WHERE listing_details.prop_mandate = 1
LIMIT 3')->fetchAll();
You don't need the second WHERE
$listings = $db->query('
SELECT *
FROM listing_details
JOIN prop_gallery
ON prop_gallery.prop_gallery_id = listing_details.prop_slug
WHERE (listing_details.prop_slug LIKE \'prop_gallery_id\' OR prop_gallery.prop_gallery_id LIKE \'prop_slug\')
AND listing_details.prop_mandate = 1
LIMIT 3')->fetchAll();

Codeigniter Active Record, Where in

I have join table with where in
I am wondering if there is a way i can say something like this :
where kd_x in (select kd_x from master_code where a='11921212')
I'm trying to do this with active record like this but is doesn't give me any data
$this->db->where_in('kd_x',array('select kd_x from MT_master where a="11921212"'));
Help me Please, Thank You
->where() you can use 2nd and 3rd argument, so that any string you can pass
$this->db->where('`kd_x` IN (select `kd_x` from `MT_master` where a="11921212")', NULL, FALSE);
OR
//Create where clause
$this->db->select('kd_x')
->where('a','11921212')
->from('MT_master');
$where_clause = $this->db->get_compiled_select();
//Create main query
$this->db->select('*');
->from('your_table');
->where("`kd_x` IN ($where_clause)", NULL, FALSE);

PHP and Mysql || 'or' syntax?

In mysql I have something like:
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5 WHERE ");
How can i do something like this below that works:
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5 WHERE `title` LIKE '%{Hello world}%' || Where `text` LIKE '%{Hello World}%'");
Something like above that will actually work.
Try this:
$queryResult = mysql_query("SELECT * FROM posts WHERE `title` LIKE '%{Hello world}%' OR `text` LIKE '%{Hello World}% LIMIT $start, 5'");
$currentName = mysql_fetch_assoc($queryResult);
if you expect many results...
while($currentName = mysql_fetch_assoc($queryResult)) {
//your code here...
}
Replace || with OR and remove the extra WHERE that comes after it. In SQL, || is OR and && is AND. You can read more here.
Note: Please take time to read manuals, it absolutely helps.
You can use OR in MySql, and it is likely very suitable for most situations.
OR however has a slight disadvantage performance wise as for each OR query the whole query is ran again. I am no MySql performance guru, but it would seem UNION is better optimized. So in general, and at lest with more OR statements you should use a UNION like this:
SELECT your_union_result.* FROM
(
SELECT * FROM posts WHERE `title` LIKE '%{Hello world}%'
UNION
SELECT * FROM posts WHERE `text` LIKE '%{Hello World}%'
) AS your_union_result
ORDER BY your_union_result.order_column
LIMIT $start, 5
Please note: My version has a subquery. You need it in case you want to order the result or limit the total rows. You now have a few options to fetch the actual result.
You can fetch_assoc(), fetch_array() and so on row by row. It is unclear for me what you want as the actual result. The $currentname indicates a post or user name, but a limit of $start, 5, indicates you want a list.
Go ahead and read http://php.net/manual/en/function.mysql-fetch-array.php and the other fetch functions. If you need more help, please update your question to be a bit more specific.

Combine multiple queries to get single sum answer

Quite new to all the SQL/PHP stuff - dabbled with basic queries and outputting them to PHP previously but now trying something a bit more complicated and hoping someone can help with this as I've been trying to work it out with no luck so far:
I have 2 MS SQL tables:
Table 1 - Faults
faultid ... requestnumber
1 ........... 6
2 ........... 5
3 ........... 6
Table 2 - actions
faultid ....who ..... when...... timetaken
1.......... John....... Mon......... 1.00
2.......... Peter...... Mon.......... 2.00
3.......... Luke....... Tues........ 1.00
2.......... John....... Tues........ 0.5
1.......... Mike....... Mon......... 0.75
What I am trying to achieve is create a variable I can use in a front end php based webpage that gets a sum of the timetaken column in Table 2 where the requestnumber in Table 1 is equal to a specific number (i.e. 6)
I'm guessing it will start with something like:
$sql1 = "select faultid FROM Faults WHERE requestnumber = '6'";
$sqlresult = sqlsrv_query($conn, $sql1);
while ($row = sqlsrv_fetch_array($sqlresult)){
}
After that I get a bit stuck. How do I take each result from this and then run another query to get the sum of the timetaken column in Table 2 for just the corresponding faultid's? I want to hazard a guess at using foreach but not sure on the syntax (or even if I'm guessing correctly).
So in this example I would get back a result of 2.75 as a variable in PHP.
Any help would be appreciated. Thanks in advance.
Andy
Best to use just one SQL-statement
$sql = 'SELECT SUM(t2.timetaken)';
$sql .= ' FROM Faults t1 INNER JOIN actions t2 ON (t2.faultid = t1.faultid)';
$sql .= ' WHERE t1.requestnumber = ?';
Use this as prepared statement and pass your requestnumber (6 or something) as argument when executing this statement.
To get all or multiple sums you can use group by (maybe combine with WHERE):
SELECT t1.requestnumber, SUM(t2.timetaken)
FROM Faults t1
INNER JOIN actions t2 ON (t2.faultid = t1.faultid)
GROUP BY t1.requestnumber
Edit:
According to your own comment, you can use a SELECT with subquery, but use IN, not =. When using '=' the subquery must return only one row.
select SUM (timetaken) FROM actions WHERE faultid IN (select Faultid from Faults WHERE requestnumber = '6')
-- ^^
But this way is usually slower than the one I posted above

SQL active record query join with where empty clause

I'm working with CodeIgniter and Active Record (mysql database), to write a special query. Here is the working query :
$this->db->select('moo_aauth_users.*', false);
$this->db->select('moo_tasks.id AS task_id, moo_tasks.user_id, moo_tasks.task_status, moo_tasks.task_type, moo_tasks.skill_category, moo_tasks.tasker_hired, moo_tasks.task_city_id, moo_tasks.completed, moo_tasks.hours_spend, moo_tasks.additional_fees, moo_tasks.total_price, moo_tasks.paid, moo_tasks.tasker_asked_id, moo_tasks.tasker_asked_response, moo_tasks.tasker_asked_rate, moo_tasks.title, moo_tasks.description, moo_tasks.expiration_date, moo_tasks.telephone, moo_tasks.zipcode, moo_tasks.address, moo_tasks.created_stamp, moo_tasks.updated_stamp, moo_tasks.assigned_stamp, moo_tasks.completed_stamp, moo_tasks.provider, moo_tasks.country, moo_tasks.task_zone', false);
$this->db->from('moo_tasks');
$this->db->join('moo_aauth_users', 'moo_aauth_users.id = moo_tasks.user_id');
$this->db->where('moo_tasks.task_status', TASK_STATUS_PENDING);
But now, I would like to add a special where clause based on another table.
I have a table moo_offers containing different offers corresponding to a task. In my query, I want to check if the task I'm retrieving DOES NOT contains a corresponding offer in the moo_offers table.
Example :
moo_tasks: id 1 / ...
moo_tasks: id 2 / ...
moo_offers: task_id 1 / ..
With the query, only moo_tasks.id = 2 will be retrieved, not the id 1 because it has an offer in the second table.
I tried multiple thinks in my query, but did'nt works :
$this->db->join('moo_offers', 'moo_offers.id_task = moo_tasks.id');
$this->db->where('moo_offers.id_tasker NOT IN (SELECT `id_tasker` FROM `moo_offers` WHERE id_task = moo_offers.id_task && id_tasker = '.$filter['tasker_id'].')', NULL, FALSE);
Have you and idea please? Thanks!
$this->db->select('*');
$this->db->from('tbl_user');
$this->db->join('tbl_userinfo', 'tbl_user.user_id = tbl_userinfo.user_id');
$this->db->where('tbl_user.user_id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
this is what i did and it worked.

Categories