Without mysql stored procedure or udf - mysql

I am trying to implement a mysql query for some report and can't figure it out.
Let's say we have a 'companies' table, a 'users' and a 'company_users'.
The requested result set is:
'company_name', 'active_users', 'date'(lets say 30 days back).
So I have to calculate user counts up to date based on users.created_at.
I could use some help to achieve it without stored procedure because I'll need to join the result set with another table.
Thank you.

Related

Using SQL to get a count of tickets done in a week? (Automated to run every week)

I would like to use SQL on a dataset and would like to get a count of the total tickets each week.(The number is taken at the end of every week)
Is there any way in SQL to run a query that will automatically run every week and the current count will be stored in another dataset?
It's simple. The entity you're looking for is events.
so your problem would be solved in following manner.
Create another table, so that you can store the ticket counts by date or week.
Create event and write ticket count query and corresponding insert query to another table.
Set proper schedule to the event and you're done.
Still I would prefer to create a procedure and then call that procedure inside the event rather than putting all logic inside event.

How to count number of rows in SQL table retrieved by a procedure [duplicate]

Is there a way to get the number of rows 'returned' from a stored procedure?
I know the result set is not really returned so I can't select from it or count on it.
I tried to use an out parameter but with no success..
Basically I have some logic in the stored procedure that finds some table lines. I use it in my C# app. in another place I need the exact same logic but only the count so I will be able to use in an SQL statement.
I could bring it to the C# and count there but I prefer not.
I could also create a stored function that duplicate the logic but returns COUNT but I prefer not to duplicate so I don't maintain it twice..
Try This
found_rows function
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
SELECT COUNT(id) AS example name FROM tablename

SQL Update with Variable as Parameter

I am working on a stored procedure in MySQL to update a row in a SQL table. What I have is a table where several of the columns are named incrementally. EX: Page_1, Page_2, Page_3....etc.
The data stored in these locations is updated at different times and I have another column to store the number of times the row has been updated. The count variable gets incremented each time the procedure runs and that allows me to utilize it's value in keeping track of where the next update of the data should take place.
From my research I keep finding solutions utilizing "Dynamic SQL." I do not understand how to utilize this to resolve my issue.
I want to pass a variable in to an update statement as the column name.
The code I currently have is as follows.
SET COUNT = COUNT + 1; -- modify count from count column
SET COLUMNLOCATIONVARIABLE = CONCAT('Page_' , COUNT); -- concatenate the count with the column "Prefix"
UPDATE Table
SET COLUMNLOCATIONVARIABLE = INPUTVARIABLE --Use the concatenated statement as the column name and update it with input data
WHERE mainID = INPUTID;
If anyone could explain to me how to pass this variable in as a column name using Dynamic SQL or another solution utilizing non-dynamic SQL I would appreciate it.
Thanks in advance.
This isn't the way to use a database. Create a new table for your Page_* columns, linked to the main table by an identity, with a page number column. Then you can update and include as many pages as you need, addressed by the main identity and the page number.

How can I get the number of rows 'returned' from a result set of a stored procedure

Is there a way to get the number of rows 'returned' from a stored procedure?
I know the result set is not really returned so I can't select from it or count on it.
I tried to use an out parameter but with no success..
Basically I have some logic in the stored procedure that finds some table lines. I use it in my C# app. in another place I need the exact same logic but only the count so I will be able to use in an SQL statement.
I could bring it to the C# and count there but I prefer not.
I could also create a stored function that duplicate the logic but returns COUNT but I prefer not to duplicate so I don't maintain it twice..
Try This
found_rows function
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
SELECT COUNT(id) AS example name FROM tablename

MySQL stored procedure parameters

I want to grab x amount of items out of the DB, I was wondering if there was a way
of passing some list of numbers to a stored procedure to be used with WHERE IN?
SELECT item_id, item_description
FROM items
WHERE item_id IN ( NEED_LIST_OF_IDS_HERE );
Should I not use a stored procedure for this query and just build up the sql in the application?
Or should I make a seperate DB call for each single item_id?
NOTE: items is not the actual name of the table, so dont bash me for a poor name choice, Im just hiding implementation.
Poor unanswered question from a year ago...
If you weren't using an IN clause, you could certainly generate/prepare the sql inside the stored procedure and execute it there. It's harder to parameterize the IN clause.
How about passing in the list of ids, make a temp table inside your stored procedure, and then join over to the items table?