ZF2 sql count rows with different values [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm developing an application in zend framework 2 ans need to get some count values from a database table.
the table contains members with a column 'active' and 'blocked'
i would like te get the total number of members as well as the number of active and the number of blocked members.
Note that there will be a big amount of members in the table, so 'slow solutions' are not a option.
thanx for the help!

With those 2 columns, you would need to do something like:
SELECT SUM(active) as nrActive, SUM(if(blocked = 'yes', 1, 0)) as nrBlocked FROM members;
In this example "active" can have value 0 or 1, for blocked I assumed 'yes' and 'no', this way you have an example of both situations.
In ZF2 you can compose a query using Zend\Sql
$sql = new \Zend\Db\Sql($dbAdapter);
$sql->from('members', array(
'nrActive' => new \Zend\Db\Sql\Expression('SUM(active)'),
'nrBlocked' => new \Zend\Db\Sql\Expression('SUM(if(blocked = "yes", 1, 0))')
);
(disclaimer: I wrote this off hand so can have typo's, just for example)

Related

Is there a way to remove specific characters from SQL Server Column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a database table that contains a column for pricing.
Its very old, so it was written before i understood datatypes. so we are using varchar for a money value.
Ive noticed some columns have $ in them, so what I'm wondering is... is there a way with SQL Server to perform an update of the table and remove any instances of non numeric characters or at the very least remove the $ from the string in the columns in one go ?
I hope this is possible.
Update tbl
SET price = replace(price, '$', '')
Here is the replace definition

finding the word like in a sql query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like this:
i would like to go to office, and i would like to go to market
My question is: how many times the word like occurs in the string?
Please write a query.
You can use regexp_count:
SELECT REGEXP_COUNT('i like to go , and i would like market', 'like')
FROM DUAL;
The word "like" occurs twice in the string you posted.
The following is a query:
SELECT *
FROM SOME_TABLE
WHERE SOME_VALUE = SOME_OTHER_VALUE
Share and enjoy.

SQL server , Add data in multi value attribute [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table Photos with attribute comments
i want to declare that comments take many values
and want to know how to insert a new value to it
You should probably have a column photo_id in your comments table. This would let you have a lot of comments for each photo.
The way you are saying it, it would let you have the same comment in different photos, but that looks less likely to be what you really want.
Something like this:
photos:
id primary key
comments
id primary_key
photo_id foreign_key referencing photos(id)

Recursive sql query for webpage URL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm using postgresql but if there's a generic sql way even better.
I have a table storing data for webpages, including slug (e.g. in example.com/x/y, x and y are both slugs) and parent_id (x and y each have a record in this table. x's id matches y's parent_id).
If I have the id for y, is there an easy way to find out the full URL (/x/y)? Or at least all the id's (/x_id/y_id)?
I know that I can get this for this particular example like this:
select parent_id from pages where slug like 'x';
=> 5
And get the slug for that id:
select slug from pages where id = (select parent_id
from pages where slug like 'x');
=> y
But what if the URI was more like /a/b/c/d/e/f/g? Is there an easy way to find all of that if all I have is the id for g?
As #a_hourse-with_no_name said a recursive CTE should take care of you. This is my favorite link for CTE's: CTE Basics

Get me a SQL quesry to check whether the two table data contents are equal? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having two table A and B of same structure.Both tables is loaded with data.I just want the mismatched row of two tables.,,
I'm certain you could do some gigantic JOIN query for each column and then issue a NOT IN over the result but a simple, pragmatic solution is to:
Write a query that orders the data on several columns (or all)
Export the query executed on each table to a separate text file
Use diff to compare the files with each other
Depending on the RDBMS vendor you may have other options you could consider as well.