How to count distinct columns query? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have to find the total number or records but count is based on distinct of multiple columns.
I have following lines in my query.
$query = $this->createQueryBuilder("j");
$query->select(
"COUNT(DISTINCT
'j.mark',
'j.model'
) as total");
But this is giving me error:
[Syntax Error] line 0, col 76: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','
Can anybody please help me solve this issue?
Thank You.

I used concat instead which worked for me. But I am not sure if its the correct solution or not.
$query->select(
"COUNT(DISTINCT(concat(j.mark, j.model))
) as total");

Related

MySQL: Count occurrences of distinct values error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to use code from the following old question:
MySQL: Count occurrences of distinct values
My query is as follows:
$result = $db->query("SELECT name,COUNT(*) as cnt FROM `table` GROUP BY name ORDER BY cnt DESC");
$row = mysqli_fetch_array($result);
var_dump($row);
In phpmyadmin this code will output all the name and COUNT columns. When I run var_dump($row) it will only have one row for me to work with (the first one, ie the one with the most occurrences), which I can't figure out why. Any ideas? Thanks.
From the documentation on mysqli_fetch_array:
Fetch a result row as an associative, a numeric array, or both
As you found out, it does just that: it fetches a row.
Maybe you were expecting the behaviour of mysqli_fetch_all?
Fetches all result rows as an associative array, a numeric array, or both

php mysql ORDER BY not working ... tried looking at others, they aren't having the same issue [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
please help ... i'm pretty knowledgeable when it comes to php mysql, but for some reason i can't figure this out.
i have this query:
$get_points = "SELECT user_id, accum_points FROM total_points ORDER BY accum_points DESC";
$run_points = mysqli_query($con, $get_points);
while($row_points = mysqli_fetch_array($run_points)) {
other stuff in here
}
and here's what is currently in the db
points_id | user_id | accum_points
_____________________________________
31 12 211.2
32 13 7.4
33 1 10.4
but it spits out it in the order of 7.4, 211.2, 10.4. i don't understand what is going on with this ... i've looked at a lot of the other questions asked about ORDER BY not working ... can anybody help?
thanks!
i made the mistake of making the column a varchar ... so that was stupid of me ... sorry about this guys

Count Days Student has Attended [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to count up the days a student has attended class.
I need it to display the total attendance based on what member_id is logged in.
This is what I have so far:
$id_member = 3;
$query = "SELECT COUNT(attendance) AS day_at FROM day_attendance WHERE id_member=$id_member";
$response = #mysqli_query($dbc, $query);
$row = mysqli_fetch_array($response);
$day_at = $row["day_at"];
echo $day_at;
It throws this error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /usr/www/user1/web/data/test.php on line 737
I think you misspelled the id column, should be member_id (as in your screen) and not id_member as in your sql query.
That's why mysqli_fetch_array print you a warning, because mysqli_query fail, and when it fail it returns a boolean (false). (Btw it's really bad to use the # character)

how to select all table records except last 2? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am using Yii, I want to select all records of a model except the last 2 records, but I got an error that there is syntax error, here is my code:
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents) FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents))";
$model2 = Newsevents::model()->findAll($c);
This one considers, that idNewsEvents might not be sequential (due to deletes or whatever).
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents NOT IN (SELECT idNewsEvents FROM newsevents ORDER BY idNewsEvents DESC LIMIT 2)";
$model2 = Newsevents::model()->findAll($c);
P.S.: Not sure if this is correct yii syntax, I'm not familiar with it, but I'm very familiar with MySQL. Anyway, you get the idea I hope.
Two checking can be done in a single checking
$c->condition ="idNewsEvents < (SELECT MAX(idNewsEvents)-1 FROM newsevents)";
Example query
SELECT * FROM `users` WHERE user_id < (SELECT MAX(user_id)-1 FROM users)
it should be:
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents)
FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents)";
There were just an extra bracket at end and a very tired eyes to notice that :( .

Mysql: Subtract one column's data from another [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I would like to subtract one column of data from another, namely gdded from gppy; both are stored in a MySQL database.
Here is my code but it seems there is something wrong. I've searched anywhere but I cannot find the right answer.
My script below
INSERT INTO denmrk
(EMPID, NAME, DATE_FR, DATE_TO, Tangkay, Alis, gppy, gdded , NET_PAY, SSS_comboE, IBG_EMPLYE, PROCESSED_DATE)
VALUES
(6666, 'JUAN DELACRUZ', '2014-02-01 00:00:00', '2014-02-01 00:00:00', 'BBBB CO', 'DRIVER', 3383.04, 300, gppy- gdded , 150, 150, GETDATE() );
With the given code and implied database structure, how can I can achieve this?
BTW there is no need to store the manipulated result in the database. You can do this when you are select the data from the database. SO it would be better to drop that col and the idea to store the calc result in the database as well.

Categories