2

My MySQL table look like this:

 'id'       'name'    'type'
 1           name1       A         
 2           name2       B 
 3           name3       A  
 4           name4       C 
 5           name5       C

How can I get all distinct types i.e A, B, C in NOTORM PHP.

sahil solanki
  • 507
  • 6
  • 20

2 Answers2

4

The API docs don't cover it off in an intuitive manner, but you need to simply use the group by function to get distinct values:

$table->group($columns[, $having])

Set GROUP BY and HAVING

This is the same (not at a db level, but the same at an output level) of running a distinct.

There isn't an anchor to the exact part, but it is in here: http://www.notorm.com/#api

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • @sahilsolanki Great, glad I could help. If this has solved the issue, it is probably a good idea to choose "Accept Answer" so that others looking this question up on Google or the like know that this solved the issue, or so that other folks lurking in the php queue don't try to come and answer an issue that is resolved. If this hasn't solved the issue, please leave a comment on what isn't working :) – Fluffeh Jul 07 '15 at 14:22
  • There is also an answer from @bisounours_tronconneuse that says you can run your own query against a table directly. It might be another valid way to get your output needed. – Fluffeh Jul 07 '15 at 14:27
2

Use the sql query

SELECT DISTINCT type FROM yourtable

using the notORM API that would be:

('SELECT DISTINCT type FROM ') $table

Another possibility is using the work arround:

$table->select('DISTINCT type')
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
  • Yeah, that works great if you are writing the SQL itself, not if you are using an API to make the call. You should look at notORM. – Fluffeh Jul 07 '15 at 14:15
  • you can execute sql queries using notORM using `('SQL QUERY STRING') $table` – patapouf_ai Jul 07 '15 at 14:19
  • Okay, I didn't see any of that in the API docs :) Perhaps you could update your answer to match the input that the notORM API could use? :) – Fluffeh Jul 07 '15 at 14:25
  • Of possible interest is also the `__construct` method: http://apigen.juzna.cz/doc/vrana/notorm/class-NotORM_Literal.html – patapouf_ai Jul 07 '15 at 15:09