I am using mysql and what I am tryng to achieve, is to select more than 1 column in a select statement. From the sample code I have posted, I can retrieve the value 'Box Retrieval' in the activity column. However, what I would like to do, is select the values, 'Box Retrieval' 'Box Return, etc, Is this possible. Thanks
$query = "SELECT * FROM act WHERE company = '".$_SESSION['kt_idcode_usr']."' AND activity = 'Box Retrieval' ORDER BY date DESC";
Try this might be helpful for you.
$query = "SELECT * FROM act WHERE activity IN ('Box Return', 'Box Retrieval', 'Box Destruction') AND company = '".$_SESSION['kt_idcode_usr']."' ORDER BY date DESC";
You can just add another condition with OR statement having the value you need -
$query = "SELECT * FROM act WHERE company = '".$_SESSION['kt_idcode_usr']."' AND activity = 'Box Retrieval' OR activity = 'Box Return' ORDER BY date DESC";
This will return all rows which have activity value either 'Box Retrieval' or 'Box Return'
You can repeat for more such cases.
Or you can use UNION of two SQL SELECT statements but I think OR should suffice in this case.
Related
In my project I am using datatables plugin with serverside processing. It works fine untill i do a search or order(sort) operation because it needs active record to do that.
My scenario is, i have an account table, revenue table and payment table, and I want to view all the data of revenue and payment table, thats why I need a union. my query is like below---
SELECT 'Income' as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created
FROM tbl_revenue JOIN tbl_accounts as ta on tbl_revenue.fld_account_id = ta.fld_id
UNION
SELECT 'Expense' as source, fld_type, fld_amount, tae.fld_account, fld_date, tbl_payment.fld_description as fld_traninfo, tbl_payment.fld_created as created
FROM tbl_payment JOIN tbl_accounts as tae on tbl_payment.fld_account_id = tae.fld_id
Is there any way to use query builder in this query?
And second question, you can see I created a virtual column named 'source', i want to filter this column using where clause with append this query like below
WHERE source like "%a%" limit(10,0)
But this returns that I don't have any column name 'source', how can I filter this column?
Any help is appreciated.
there is a way to do that but its a bit hacky because codeigniter's querybuilder adds an auto SELECT statement to the query if you didn't specify it by yourself
In order to get what you want, you've to split your select statements in 2 queries and add the where clause to this query
Something like that should work:
$strQuery1 = $this->db
->select('income as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_revenue')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strQuery2 = $this->db
->select('Expense as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_payment')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strWhere = substr($this->db->like('source', 'a', 'both')->get_compiled_select(), 8);
$query = $this->db->query($strQuery1.' UNION '.$strQuery2.$strWhere);
I have a table with Invoice Date and Customer ID (among other data, of course). I'm looking to display the longest invoice date in the future in the table for each customer.
I am currently using this MySQL in the WHERE:
WHERE o70vm_invoices_invoices.invoice_date = (SELECT MAX(o70vm_invoices_invoices.invoice_date) FROM o70vm_invoices_invoices)";
This code currently displays the oldest date in the column for just one customer.
I'm including my entire MySQL query below, please keep in mind that I'm having to pull from various tables to display the data I need.
$query = "SELECT
o70vm_invoices_contacts.name AS 'Parent_Name',
o70vm_invoices_contacts.id AS 'Parent_ID',
o70vm_invoices_contacts.company AS 'Children',
o70vm_invoices_contacts.active AS 'Active',
o70vm_invoices_invoices.id AS 'Invoice_ID',
o70vm_invoices_invoices.user_id AS 'Parent_ID_on_Invoice',
o70vm_invoices_invoices.invoice_num AS 'Invoice_Num',
o70vm_invoices_invoices.invoice_date AS 'Invoice_Date',
o70vm_invoices_invoices.invoice_duedate AS 'Invoice_Date_Due',
o70vm_invoices_payments.invoice_id,
o70vm_invoices_payments.id,
o70vm_invoices_payments.payment_amount AS 'Payment_Amount',
o70vm_invoices_payments.payment_datetime AS 'Payment_Date',
o70vm_invoices_payments.payment_duedate,
o70vm_invoices_payments.payment_status AS 'Payment_Status',
o70vm_invoices_payments.payment_description AS 'Payment_Descript',
o70vm_invoices_payments.payment_type AS 'Payment_Type'
FROM o70vm_invoices_invoices, o70vm_invoices_contacts, o70vm_invoices_payments
WHERE
o70vm_invoices_contacts.id = o70vm_invoices_invoices.user_id
AND
o70vm_invoices_payments.invoice_id = o70vm_invoices_invoices.id
AND
o70vm_invoices_invoices.invoice_date < '2099-01-01'
AND
o70vm_invoices_contacts.active = 1
AND o70vm_invoices_invoices.invoice_date = (SELECT MAX(o70vm_invoices_invoices.invoice_date) FROM o70vm_invoices_invoices)";
If I use a GROUP BY:
GROUP BY o70vm_invoices_contacts.id
I can pull each customer to display but it does not display the last future invoice in the table.
Based on Stefan's comments below, here is my revised subquery:
$query = "SELECT *
FROM ( SELECT
o70vm_invoices_contacts.name AS 'Parent_Name',
o70vm_invoices_contacts.id AS 'Parent_ID',
o70vm_invoices_contacts.company AS 'Children',
o70vm_invoices_contacts.active AS 'Active',
o70vm_invoices_invoices.id AS 'Invoice_ID2',
o70vm_invoices_invoices.user_id AS 'Parent_ID_on_Invoice',
o70vm_invoices_invoices.invoice_num AS 'Invoice_Num',
o70vm_invoices_invoices.invoice_date AS 'Invoice_Date',
o70vm_invoices_invoices.invoice_duedate AS 'Invoice_Date_Due',
o70vm_invoices_payments.invoice_id,
o70vm_invoices_payments.id,
o70vm_invoices_payments.payment_amount AS 'Payment_Amount',
o70vm_invoices_payments.payment_datetime AS 'Payment_Date',
o70vm_invoices_payments.payment_duedate,
o70vm_invoices_payments.payment_status AS 'Payment_Status',
o70vm_invoices_payments.payment_description AS 'Payment_Descript',
o70vm_invoices_payments.payment_type AS 'Payment_Type'
FROM o70vm_invoices_invoices, o70vm_invoices_contacts, o70vm_invoices_payments
WHERE
o70vm_invoices_contacts.id = o70vm_invoices_invoices.user_id
AND
o70vm_invoices_payments.invoice_id = o70vm_invoices_invoices.id
AND
o70vm_invoices_invoices.invoice_date < '2099-01-01'
AND
o70vm_invoices_contacts.active = 1
ORDER BY o70vm_invoices_invoices.invoice_date DESC
) AS tmp
GROUP BY tmp.id";
This query lists the first invoice date and not the last invoice date. If I change the order to ASC, it just sorts the results of the first invoice date.
Any help, much appreciated.
Best way might be to make use of a sub query:
SELECT *
FROM (
SELECT *
FROM o70vm_invoices_invoices
ORDER BY invoice_date DESC
) AS tmp
GROUP BY tmp.id
I'm sure there is a better way to do this; however, this works.
I just added MAX inside my query as such:
max(o70vm_invoices_invoices.invoice_date) AS 'Invoice_Date',
and this lists the furthest date for each customer.
I'm trying to convert this SQL query into a codeigniter query
SELECT
uploads.EMAIL
FROM
uploads
JOIN (
SELECT EMAIL, COUNT(*) as num FROM uploads GROUP BY EMAIL
) c ON uploads.EMAIL = c.EMAIL
ORDER BY
c.num DESC,
EMAIL ASC
Thanks for the help
kind regards
I am not sure why you can't figure this out yourself using the active record documentation, but:
$this->db->select('uploads.EMAIL');
$this->db->from('uploads');
$this->db->join('(SELECT EMAIL, COUNT(*) as num FROM uploads GROUP BY EMAIL) c','uploads.EMAIL = c.EMAIL','',FALSE);
$this->db->order_by('c.num desc, uploads.EMAIL asc');
and then
$query = $this->db->get();
FYI, passing FALSE as the fourth parameter to the db->join() method will cause it not to escape the statement, so you should be careful if you're going to take external variables. This is, until CodeIgniter 3, the only way to do subqueries with active record without extending the active record class to add them.
I have a messaging system (very basic) that has a table like this:
**MESSAGE_ID** **RUSER_ID** **SUSER_ID** **MESSAGE_DATA** **DATE**
RUSER is the receiving user, and SUSER is the sending user. If I wanted to output a query that would output a certain users messages, I would currently do:
Select * from PRIVATE_MESG where RUSER_ID=$USER_ID or SUSER_ID=$USER_ID
That would give me all message_id's that are associated with that USER_ID. What I would like, is to create a column that would produce only the ID associated with RUSER_ID or SUSER_ID associated with a specific user. I need it to choose the messages that RUSER_ID or SUSER_ID are equal to a USER_ID but only display the one that isn't USER_ID
I would then like to do a group by the output of that query.
Any help is greatly appreciated!
Thanks!
update I am not really looking for a message_id, I am just looking for a list of users who that person has written to or received from.
UPDATE
Just so everyone knows, I recieved the answer to this question perfectly! I tweaked it later on so that it would also display them by date from newest to oldest. I did this by spliting the DATETIME into DATE and TIME USING the DATE() and TIME() Function. Here was my final query:
SELECT
IF(RUSER_ID = $USER, SUSER_ID, RUSER_ID) as THE_OTHER_GUY, DATE(DATE) as DAY, TIME(DATE) as TIME
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER
OR SUSER_ID = $USER;
group by THE_OTHER_GUY ORDER BY DAY DESC, TIME DESC
Hope this helps the next person!
You can query:
SELECT
*,
IF(RUSER_ID = $USER_ID, SUSER_ID, RUSER_ID) as THE_OTHER_GUY
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER_ID
OR SUSER_ID = $USER_ID;
SELECT SUSER_ID FROM PRIVATE_MESG WHERE RUSER_ID=$USER_ID
UNION
SELECT RUSER_ID FROM PRIVATE_MESG WHERE SUSER_ID=$USER_ID
It retrieves:
- the list of user IDs who sent messages to $USER_ID
- the list of user IDs who received messages from $USER_ID
And UNION groups the 2 lists in a single result set.
I have come across a scenario where I need to "cast" the output of a function as the column name I want to select:
(SELECT
LOWER(DATE_FORMAT(NOW(), '%b'))
FROM lang_months
WHERE langRef = lang_statements.langRef
) AS month
Just returns the current month which is expected, but I want this to select the column called "may" in this case.
How would I do this?
Thanks, your answer gave me an idea. I just put the current date into a variable and used that in the query like so:
$thisMonth = strtolower(date('M')) ;
(SELECT
$thisMonth
FROM lang_months
WHERE langRef = lang_statements.langRef
) AS month
This is not possible. The name of an entity must be known when the query reaches MySQL.
The easiest option would probably be to determine the column name in whatever language you're using then to just use that. For example, in PHP:
$col = 'someAlias';
$query = "SELECT blah as `{$col}` FROM tbl";
I don't think this is possible.
You could create a view that offers this view on your data so you can they query it more expressively, but you're still going to have to write those 12 subqueries and aliases by hand.
This should work:
$month = LOWER(DATE_FORMAT(NOW(), '%b')); // Results in 'may'
$result = mysql_query('SELECT * FROM $month'); // Returns all records in table 'may'