Zend db Mysql sorting by count [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm looking for the right syntax using Zend db to show a table that is sorted by the count. For example, my mysql table looks like this:
Users Description
1 topic1
1 topic2
2 topic3
I want the output to look like:
User 1 (2 descriptions)
User 2 (1 description)

The raw query you're looking for
SELECT users, COUNT(*) count
FROM table1
GROUP BY users
ORDER BY COUNT(*) DESC
Output:
| USERS | COUNT |
|-------|-------|
| 1 | 2 |
| 2 | 1 |
Here is SQLFiddle demo
I'm not an expert in Zend but your query may look something between the lines of
$select = $db->select()
->from('table1', array('users', 'count' => '(COUNT(*))'))
->group('users')
->order('(COUNT(*)) DESC');
$stmt = $db->query($select);
$result = $stmt->fetchAll();

Related

How to set display multiple data under one perticular field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This is my sql outcome:
What I am trying to achieve is to show the garden and under that it shows the hose pipe ,lawn moar etc.
How to write the query?
If I understand you correct you would normaly do that somehow like that.
cat_id | parent_id | cat_name | cat order
1 0 Garden 1
2 1 Hose Pipe 1
3 1 Lawn Moar 2
in this case Garden for example would have the cat_id 1 and parent_id 0 and all subcategories have their own incremented id and the parent_id of garden, which would be 1 in this case!
then you can query for your categories and foreach of them query for the subcategories where the parent_id is the cat_id of the maincategory!
here you have a nice tutorial on how to display it recursive:
https://www.codexworld.com/dynamic-category-subcategory-tree-php-mysql/
that way you also only need to save the imagepath only once in your maincat!

How to store daily hits for items using MySQL and PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my MySQL database, I'm trying way of creating a table to track daily views on 100 items. Each day would insert a new row for each item. So the table columns would look like this:
| ItemID | ItemName | Hits |
Is there a way to store daily hits for items?
What you could do is have a table with a structure like:
| item_hits |
|---------------|
| itemID | date |
Then your SQL query could look like
SELECT count(*) FROM item_hits WHERE date = '2015/04/15' and itemID = 7;
You wouldn't need to save the itemName in the table, because you should be able to get the itemName by joining it to your items table.

Select only one number from array MySQL PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have the following query bellow
SELECT * FROM subcat where catid in (2) order by id desc
what i am trying to do is select one number only in catid i have ex:
1,2,3,4,5
this work but only when "catid" start with number "2" then have ","
example
ID | 1
subcat_name | PHP
catid | 2,3,4
ID | 2
subcat_name | ASP
catid | 5,2,3
ID | 3
subcat_name | MYSQL
catid | 6
ID | 4
subcat_name | C++
catid | 2
i want to select the subcat with only catid = 2
Try this
SELECT * FROM subcat where FIND_IN_SET(2,catid) order by id desc

Get the place for the user entries [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am really not good at math, but can someone help me though how i can get the place the user has on all entries? Etc. "You are now number 43 out of 20.403.044 entries"?
SELECT ?myPlace? FROM entries WHERE userid = 1 ORDER BY time ASC
ok, i think you want
SELECT COUNT(*) FROM TABLE WHERE id <= YOUR_USER_ID
You can get the rank with this query:
SELECT COUNT(*) + 1
FROM entries entries1
INNER JOIN entries entries2 ON (entries1.id != entries2.id AND entries2.time < entries1.time)
WHERE entries1.id = 4
You just need to count the number of people with a better time, and add 1 (since the first rank is 1).
Unfortunately MySQL lacks windowing functions. Therefore you have emulate them. One way to do it is something like this
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place
FROM entries e
WHERE userid = 1
Sample output:
| USERID | PLACE |
|--------|-------|
| 1 | 2 |
If you need total number of entries in the same query
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place,
(
SELECT COUNT(*)
FROM entries
) total
FROM entries e
WHERE userid = 1;
Sample output:
| USERID | PLACE | TOTAL |
|--------|-------|-------|
| 1 | 2 | 5 |
Here is SQLFiddle demo

select an entry with specified id, and also one entry before and after with the same foreign key value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table which looks like this one:
| id | fk_book | name |
-----------------------
| 1 | 2 | test1|
| 3 | 2 | test3|
| 6 | 3 | notes|
| 7 | 2 | test2|
No I want to get the entry with the id 3. select * from test where id=3 AND fk_book=1;
but is there also a way to get the item with id 1 and 7?
and I dont know the ids of the other entries
thanks
I think you need this:
select * from test where id=3 AND fk_book = 2
union all
select * from test where id < 3 AND fk_book = 2 order by id desc limit 1
union all
select * from test where id > 3 AND fk_book = 2 order by id asc limit 1
Try following sql query:
select * from test where id in(1,3,7);

Categories