Sort by reference count php - php

How can I sort by reference count?
In the above picture,
2-1
1-1
I want to go out like this.
I'm sorry for my bad english

To sort a result set by a column you would append an ORDER clause to your query.
For example:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Bowser | 1989-08-31 |
| Fang | 1990-08-27 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+
More information can be found in the manual:
https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html

Related

multiple or single selection

i have two tables and column name are as :
Table 1
user | food | color | bike | car
Table 2
user | mobile | laptop
Now i want to get result by select single or multiple value.
For example, if i want select user which have bike and laptop . then i can get result it by query but for this all fields i have to use many condition . i have used if else where. and i also want to refine select with current selection . so what should i use ? Please help my previous question was same but i did not asked perfectly. so asked again. Thank You.
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.
Example
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-24 |
| 2 | Learn MySQL | Abdul S | 2007-05-24 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
-> FROM tutorials_tbl a, tcount_tbl b
-> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

Codeigniter join multiple tables with where clauses

I have four tables that I need to join together, with one of those tables having two different records I need to grab. The guild and party tables I'm getting the name of those so that I can display the name instead of ID to the user, and the charlog table has the char_id's creation date and last login date that I want to show as well. Here are the tables:
char table:
----------------------------------------------------
| char_id | name | guild_id | party_id |
|---------------------------------------------------
| 150000 | char1 | 3 | 3 |
| 150001 | char2 | 2 | (NULL) |
| 150002 | char3 | (NULL) | 1 |
| 150003 | char4 | 1 | 2 |
----------------------------------------------------
guild table:
-------------------------
| guild_id | name |
-------------------------
| 1 | guild_1 |
| 2 | guild_2 |
| 3 | guild_3 |
-------------------------
party table:
-------------------------
| party_id | name |
-------------------------
| 1 | party_1 |
| 2 | party_2 |
| 3 | party_3 |
-------------------------
charlog table:
----------------------------------------------------
| time | char_id | char_msg |
----------------------------------------------------
| 2015-02-14 06:45:32 | 150000 | make new char |
| 2015-02-14 06:45:58 | 150000 | char select |
| 2015-02-15 12:32:19 | 150001 | make new char |
| 2015-02-15 16:54:01 | 150000 | char select |
| 2015-02-15 19:23:54 | 150001 | char select |
| 2015-02-16 01:32:13 | 150002 | make new char |
| 2015-02-16 01:33:01 | 150003 | make new char |
| 2015-02-16 04:45:43 | 150000 | char select |
| 2015-02-16 07:43:22 | 150003 | char select |
----------------------------------------------------
As mentioned, I need to get the make new char entry from the charlog table to display when the character was created and as well the LAST (by date) char select entry to display when the character was played, all of this with one single char_id.
All in all, I'd be looking for a table that looks like this:
---------------------------------------------------------------------------------------------------------------------
| char_id | name | guild_id | guild_name | party_id | party_name | create_time | lastlogin_time |
---------------------------------------------------------------------------------------------------------------------
| 150000 | char1 | 3 | guild_3 | 3 | party_3 | 2015-02-14 06:45:32 | 2015-02-16 04:45:43 |
I'm using the following active record lines in Codeigniter to attempt to get the data I need. It returns the create_date correctly but the lastlogin_time is not returned (it's blank):
function get_char_info($cid) {
$this->db->select('char.*,guild.guild_id,guild.name AS guild_name,party.party_id,party.name AS party_name,charlog1.time AS create_time,charlog2.time AS lastlogin_time');
$this->db->from('char');
$this->db->where('char.char_id', $cid);
$this->db->join('guild', 'char.guild_id = guild.guild_id', 'left');
$this->db->join('party', 'char.party_id = party.party_id', 'left');
$this->db->join('charlog AS charlog1', 'char.char_id = charlog1.char_id AND charlog1.char_msg = "make new char"', 'left');
$this->db->join('charlog AS charlog2', 'char.char_id = charlog2.char_id AND charlog2.char_msg = (SELECT max(charlog.time) FROM charlog WHERE char_msg = "char select")', 'left');
$query = $this->db->get();
return $query->row();
}
As mentioned, the guild_name, party_name and create_time come through correctly, but the lastlogin_time is blank, no error.
I've tried jumbling some things around in the active record clauses but can't get the lastlogin_time to show. Any help would be appreciated.
The join on your subquery is not correct :
It should be charlog2.time = (SELECT....) not charlog2.char_msg = (SELECT....)

How to count number of rows with the same column data and display to table?

I have 2 tables, the 'department' and 'document'.
Table department
| doc_id | dept_name |
----------------------------------
| 1 | Information Technology|
| 2 | Software Development |
| 3 | Human Resource |
| 4 | Accounting |
| 5 | Support |
Table document
| doc_id | doc_name | author | description | department |
----------------------------------------------------------------------------
| 1 | Maps | User1 | sample | Information Technology |
| 2 | Audits | User3 | sample | Software Development |
| 3 | Image | User1 | sample | Information Technology |
| 4 | Papers | User4 | sample | Human Resource |
| 5 | Print Screen| User1 | sample | Software Development |
| 6 | Transaction | User3 | sample | Accounting |
| 7 | Graph | User1 | sample | Support |
| 8 | Excel | User1 | sample | Information Technology |
Now, I want to display the table with two columns: department and total_doc.
Output:
| department |total_doc|
-----------------------------------
| Information Technology| 3 |
| Software Development | 2 |
| Human Resource | 1 |
| Accounting | 1 |
| Support | 1 |
I want to display the total document inside the department and arrange them in ascending order.
Here's my query.(not sure)
SELECT department, count(doc_name) as 'total_doc' FROM tbl_document GROUP BY doc_name
I'm using MVC pattern in Codeigniter.
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('doc_name');
Also, How can I display this in table? like using foreach in html?
You need to do group by with department not with doc_name.
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('department');
$result = $this->db->get()->result();
Hope This will help you.
foreach ($result as $row)
{
echo $row->department."----".$row->total_doc;
}
here you go
SELECT dept_name,COUNT(td.department) FROM department d
LEFT JOIN tdocument td ON td.`department`=d.`dept_name`
GROUP BY td.`department` ORDER BY COUNT(td.`department`) DESC;
You want one line per department. IN SQL words: You want to group by department.
select department, count(*) as total_doc from document group by department;
(BTW: don't use single quotes for column aliases.)

Get all dbpprops from a certain box in wikipedia

Let me use these 2 links as my examples:
http://en.wikipedia.org/wiki/Maarten_Stekelenburg
http://dbpedia.org/page/Maarten_Stekelenburg
What I want to do is get all the dbpprops which are in the top right infobox (the one with the picture).
Here I encounter 2 problems:
How do I get all the properties without knowing them (We are putting together a football player database, and the prop names are all different for every player)
How do I make sure those are the properties that are inside the box, because there are many other dbpprops on that page and it doesn't look like dbpedia differentiates between those.
After that I want to add all those properties to an array(if that is possible).
If it's possible, I would like it to look something like this:
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX dbp2: <http://dbpedia.org/ontology/>
PREFIX dbp3: <http://dbpedia.org/property/>
SELECT *
WHERE {
dbp:".$term." dbp2:abstract ?abstract .
dbp:".$term." dbp2:thumbnail ?img .
dbp:".$term." dbp3:* ?properties . //This would then be replaced by the necessary line
FILTER langMatches(lang(?abstract), 'en')
}
EDIT: I am working with PHP
I can't guarantee that the set of properties from the infobox will be exactly the properties that are dbpprop properties (as opposed to dbpedia-owl properties), but it looks like there's a fairly good correspondence. In this case, you could use a query like the following which asks for properties and values of the person you mentioned, but only thoe properties that are in the dbpprop namespace.
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpprop: <http://dbpedia.org/property/>
select ?property ?value where {
dbpedia:Maarten_Stekelenburg ?property ?value
filter( strstarts(str(?property),str(dbpprop:)) )
}
SPARQL results
----------------------------------------------------------------------------------------------------------------------
| property | value |
======================================================================================================================
| dbpprop:bg | "gold"#en |
| dbpprop:bg | "#F1771D"#en |
| dbpprop:birthDate | "1982-09-21+02:00"^^<http://www.w3.org/2001/XMLSchema#date> |
| dbpprop:birthPlace | "Haarlem, Netherlands"#en |
| dbpprop:caps | 44 |
| dbpprop:caps | 191 |
| dbpprop:clubnumber | 24 |
| dbpprop:clubs | dbpedia:A.S._Roma |
| dbpprop:clubs | dbpedia:AFC_Ajax |
| dbpprop:currentclub | dbpedia:A.S._Roma |
| dbpprop:dateOfBirth | 22 |
| dbpprop:fg | "navy"#en |
| dbpprop:fg | "white"#en |
| dbpprop:fullname | "Maarten Stekelenburg"#en |
| dbpprop:goals | 0 |
| dbpprop:name | "Maarten Stekelenburg"#en |
| dbpprop:name | "Stekelenburg, Maarten"#en |
| dbpprop:nationalcaps | 4 |
| dbpprop:nationalcaps | 54 |
| dbpprop:nationalgoals | 0 |
| dbpprop:nationalteam | dbpedia:Netherlands_national_football_team |
| dbpprop:nationalteam | dbpedia:Netherlands_national_under-21_football_team |
| dbpprop:nationalyears | 2002 |
| dbpprop:nationalyears | 2004 |
| dbpprop:ntupdate | 18 |
| dbpprop:pcupdate | 21 |
| dbpprop:placeOfBirth | "Haarlem, Netherlands"#en |
| dbpprop:position | <http://dbpedia.org/resource/Goalkeeper_(association_football)> |
| dbpprop:shortDescription | "Dutch footballer"#en |
| dbpprop:title | "Awards"#en |
| dbpprop:title | "Netherlands squads"#en |
| dbpprop:years | 2002 |
| dbpprop:years | 2011 |
| dbpprop:youthclubs | dbpedia:AFC_Ajax |
| dbpprop:youthclubs | "Schoten"#en |
| dbpprop:youthclubs | "Zandvoort '75"#en |
| dbpprop:youthyears | 1997 |
| dbpprop:wordnet_type | <http://www.w3.org/2006/03/wn/wn20/instances/synset-soccer_player-noun-1> |
| dbpprop:hasPhotoCollection | <http://wifo5-03.informatik.uni-mannheim.de/flickrwrappr/photos/Maarten_Stekelenburg> |
----------------------------------------------------------------------------------------------------------------------
You mentioned storing results in an array, but I don't really understand the example that you've provided. It looks more like a query than code to put something into an array. Additionally, you haven't specified what programming language you're working with, so we can't really respond with array manipulation code.

Join 2 children tables with a parent tables without duplicated

Problem
I have 3 tables: People, Phones and Emails. Each person has an UNIQUE ID, and each person can have multiple numbers or multiple emails.
Simplified it looks like this:
+---------+----------+
| ID | Name |
+---------+----------+
| 5000003 | Amy |
| 5000004 | George |
| 5000005 | John |
| 5000008 | Steven |
| 8000009 | Ashley |
+---------+----------+
+---------+-----------------+
| ID | Number |
+---------+-----------------+
| 5000005 | 5551234 |
| 5000005 | 5154324 |
| 5000008 | 2487312 |
| 8000009 | 7134584 |
| 5000008 | 8451384 |
+---------+-----------------+
+---------+------------------------------+
| ID | Email |
+---------+------------------------------+
| 5000005 | Smithley#goodmail.com.com |
| 5000005 | Smithley.j#gmail.com |
| 5000008 | Smithley#gmail.com |
| 5000008 | tech#goodmail.com |
| 5000008 | feler#campus.uni.com |
| 8000009 | Ashley.hill86#gmail.com |
| 5000004 | georgestanko#hotmail.com |
+---------+------------------------------+
I am trying to joining them together without duplicates. It works great, when I try to join only Emails with People or only Phones with People.
SELECT People.Name, People.ID, Phones.Number
FROM People
LEFT OUTER JOIN Phones ON People.ID=Phones.ID
ORDER BY Name, ID, Number;
+----------+---------+-----------------+
| Name | ID | Number |
+----------+---------+-----------------+
| Steven | 5000008 | 8451384 |
| Steven | 5000008 | 24887312 |
| John | 5000005 | 5551234 |
| John | 5000005 | 5154324 |
| George | 5000004 | NULL |
| Ashley | 8000009 | 7134584 |
| Amy | 5000003 | NULL |
+----------+---------+-----------------+
SELECT People.Name, People.ID, Emails.Email
FROM People
LEFT OUTER JOIN Emails ON People.ID=Emails.ID
ORDER BY Name, ID, Email;
+----------+---------+------------------------------+
| Name | ID | Email |
+----------+---------+------------------------------+
| Steven | 5000008 | Smithley#gmail.com |
| Steven | 5000008 | tech#goodmail.com |
| Steven | 5000008 | feler#campus.uni.com |
| John | 5000005 | Smithley#goodmail.com.com |
| John | 5000005 | Smithley.j#gmail.com |
| George | 5000004 | georgestanko#hotmail.com |
| Ashley | 8000009 | Ashley.hill86#gmail.com |
| Amy | 5000003 | NULL |
+----------+---------+------------------------------+
However, when I try to join Emails and Phones on People - I get this:
SELECT People.Name, People.ID, Phones.Number, Emails.Email
FROM People
LEFT OUTER JOIN Phones ON People.ID = Phones.ID
LEFT OUTER JOIN Emails ON People.ID = Emails.ID
ORDER BY Name, ID, Number, Email;
+----------+---------+-----------------+------------------------------+
| Name | ID | Number | Email |
+----------+---------+-----------------+------------------------------+
| Steven | 5000008 | 8451384 | feler#campus.uni.com |
| Steven | 5000008 | 8451384 | Smithley#gmail.com |
| Steven | 5000008 | 8451384 | tech#goodmail.com |
| Steven | 5000008 | 24887312 | feler#campus.uni.com |
| Steven | 5000008 | 24887312 | Smithley#gmail.com |
| Steven | 5000008 | 24887312 | tech#goodmail.com |
| John | 5000005 | 5551234 | Smithley#goodmail.com |
| John | 5000005 | 5551234 | Smithley.j#gmail.com |
| John | 5000005 | 5154324 | Smithley#goodmail.com |
| John | 5000005 | 5154324 | Smithley.j#gmail.com |
| George | 5000004 | NULL | georgestanko#hotmail.com |
| Ashley | 8000009 | 7134584 | Ashley.hill86#gmail.com |
| Amy | 5000003 | NULL | NULL |
+----------+---------+-----------------+------------------------------+
What happens is - if a Person has 2 numbers, all his emails are shown twice (They can not be sorted! which means they can not be removed by #last)
What I want:
Bottom line, playing with the #last, I want to end up with somethig like this, but #last won't work if I don't arrange ORDER columns in the righ way - and this seems like a big problem..Orderin the email column. Because seen from the example above:
Steven has 2 phone number and 3 emails. The JOIN Emails with Numbers happens with each email - thus duplicated values that can not be sorted (SORT BY does not work on them).
**THIS IS WHAT I WANT**
+----------+---------+-----------------+------------------------------+
| Name | ID | Number | Email |
+----------+---------+-----------------+------------------------------+
| Steven | 5000008 | 8451384 | feler#campus.uni.com |
| | | 24887312 | Smithley#gmail.com |
| | | | tech#goodmail.com |
| John | 5000005 | 5551234 | Smithley#goodmail.com |
| | | 5154324 | Smithley.j#gmail.com |
| George | 5000004 | NULL | georgestanko#hotmail.com |
| Ashley | 8000009 | 7134584 | Ashley.hill86#gmail.com |
| Amy | 5000003 | NULL | NULL |
+----------+---------+-----------------+------------------------------+
Now I'm told that it's best to keep emails and number in separated tables because one can have many emails. So if it's such a common thing to do, what isn't there a simple solution?
I'd be happy with a PHP Solution aswell.
What I know how to do by now that satisfies it, but is not as pretty.
If I do it with GROUP_CONTACT I geat a satisfactory result, but it doesn't look as pretty: I can't put a "Email type = work" next to it.
SELECT People.Ime,
GROUP_CONCAT(DISTINCT Phones.Number),
GROUP_CONCAT(DISTINCT Emails.Email)
FROM People
LEFT OUTER JOIN Phones ON People.ID=Phones.ID
LEFT OUTER JOIN Emails ON People.ID=Emails.ID
GROUP BY Name;
+----------+----------------------------------------------+---------------------------------------------------------------------+
| Name | GROUP_CONCAT(DISTINCT Phones.Number) | GROUP_CONCAT(DISTINCT Emails.Email) |
+----------+----------------------------------------------+---------------------------------------------------------------------+
| Steven | 8451384,24887312 | Smithley#gmail.com,tech#goodmail.com,feler#campus.uni.com |
| John | 5551234,5154324 | Smithley#goodmail.com,Smithley.j#gmail.com |
| George | NULL | georgestanko#hotmail.com |
| Ashley | 7134584 | Ashley.hill86#gmail.com |
| Amy | NULL | NULL |
+----------+----------------------------------------------+---------------------------------------------------------------------+
What you want isn't actually what you want at all, if that makes any sense... you can't realistically do anything programatically with your database output, you need to do something with it (unless you're just running a query directly on your database).
And since you stated "I'd be happy with a PHP Solution aswell." ... what you really want is something like a PHP "User" object, something like (this is all hypothetical of course):
<?php
class User {
private $_id;
private $_telNos = array();
private $_emails = array();
public function __construct($iUserId = null, $oDatabaseAbstractionObject = null) {
if(!is_null($iUserId)) $this->setId($iUserId);
if(!is_null($oDatabaseConnectionObject)) $this->load($iUserId, $oDatabaseAbstractionObject);
}
public setId($iUserId) {
$this->_id = (int) $iUserId;
}
public getId() {
return $this->_id;
}
/* telephone and email setters and getters */
public function load($iUserId, $oDatabaseAbstractionObject) {
/* error trapping - for example if $iUserId is null */
$this->setTelNos($oDatabaseAbstractionObject->readTelNos($iUserId));
$this->setEmails($oDatabaseAbstractionObject->readEmails(iUserId));
}
}
?>
Your database abstraction object then just needs to do some very simple queries to read your user, user email and phone number tables and return the results as arrays which you can then put straight into your PHP object. For example:
<?php
/**
* this implements a database connection object as a private class member
*/
class DBUser {
private $_conn;
/* constructor other functionality */
/**
* method to pass an SQL query to the database and return an array of results
*/
public function readTelNos($iUserId) {
return $this->_conn->read("SELECT `number` from `tel` WHERE `user_id` = " . (int) $iUserId);
}
}
?>
This breaks your problem down into smaller, easier to handle problems, and wraps them all up in nice PHP objects that you can then actually work with.
Your user object will have a list if phone numbers and emails that can easily be retrieved with something like $oUser->getEmails(); and if they're stored as an associative array inside the object you could even retrieve them by "label" $oUser->getEmail('work');
If I've understand correctly you also need to associate other fields to email (eg: type).
Using group_concat you can add the email type to the results, in my example ";" separate mail from type and "," separate the results.
SELECT u.name,
GROUP_CONCAT( distinct p.phone),
GROUP_CONCAT( distinct 'mail=',e.mail,';type=' ,type)
FROM people u
LEFT OUTER JOIN phone p ON u.id=p.id
LEFT OUTER JOIN mail e ON u.id=e.id
GROUP BY u.id
Fist create temporary table Name temp_tb than insert record. Then make partition using Row_Number() and delete all the record that list more than once. Like given blow.
Insert into temp_tb SELECT People.Name, People.ID, Phones.Number, Emails.Email
FROM People
LEFT OUTER JOIN Phones ON People.ID = Phones.ID
LEFT OUTER JOIN Emails ON People.ID = Emails.ID
ORDER BY Name, ID, Number, Email;
SELECT * from temp_tb
With A as
(
select People.Name, People.ID, Phones.Number, Emails.Email ROW_NUMBER()
OVER (Partition by ID ORDER BY Fee_temp.Name) As Number from temp_tb
)
DELETE FROM A WHERE Number>1
SELECT * FROM temp_tb
Hope it helps you.

Categories