What I'm trying to do is search through my table and count the instances of a letter, and store the ID of each instance in mysql and php (PDO).
What I had been messing around with is this:
$user = 'john';
$stmt = $pdo->prepare("SELECT id, SUBSTR(surname, 1, 1) as surname_init,COUNT(*) as count FROM first_table WHERE user = :user GROUP BY surname_init ORDER BY count DESC");
$stmt->bindValue(":user", $user);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($row);
This is the result:
array (size=2)
0 =>
array (size=3)
'id' => string '3' (length=1)
'surname_init' => string 'B' (length=1)
'count' => string '2' (length=1)
1 =>
array (size=3)
'id' => string '7' (length=1)
'surname_init' => string 'D' (length=1)
'count' => string '1' (length=1)
So I have two results: B has 2 instances, D has one. However "B" has only one resulting id returned from two.
Is this possible? I think I'm missing something here..
Use GROUP_CONCAT() to get all rows that matched the group by, like:
SELECT
GROUP_CONCAT(id) AS id_list,
SUBSTR(surname, 1, 1) as surname_init,
COUNT(*) as count
FROM first_table
WHERE user = :user
GROUP BY surname_init
ORDER BY count DESC
See if this helps:
$user = 'john';
// use this to get unique starting characters for surnames for the user John
$stmt = $pdo->prepare("SELECT SUBSTR(surname, 1, 1) as `surname_init`,
COUNT(*) as `count` FROM first_table
WHERE user = :user GROUP BY
surname_init ORDER BY count DESC");
$stmt->bindValue(":user", $user);
$stmt->execute();
$rows1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows1 as $row1){
echo "<BR>Starting Letter: " . $row1['surname_init'];
echo "<BR>Count: " . $row1['count'];
// now get all the IDs for this user that have the
// surnames starting with this alphabet
$stmt = $pdo->prepare("SELECT ID, SUBSTR(surname, 1, 1) as `surname_init`,
FROM first_table
WHERE user = :user
AND surname LIKE :surname");
$stmt->bindValue(":user", $user);
$stmt->bindValue(":surname", $row1['surname_init']."%");
$stmt->execute();
$rows2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows2 as $row2){
// do insert based on ID here
}
}
Related
I have two tables named "stats" and "users"
users table has all the typical user data like id,username,password,email(columns)
stats table has id,attack, defense,ostats,gold,food(columns)
I want to display data from these two tables side by side and have the data linked through their IDS
For example,
Rank user_uid ostats attack defense gold
1 Test 10 5 5 100
2 Test2 8 2 6 60
3 Test3 6 5 1 40
Username is from table "users" and the rest of them are from table "stats"
So first I want to know how to link and display the data from the same ID, like Username(user_id=1) and ostats,attack,defense,gold,food(id=1)
Then I want them in order by their "ostats" (I don't have a column named "rank" in any table yet, just don't know how to create the rank using overall stats)
You could do something like (untested)
SELECT u.username, s.overall, s.attack, s.defense, s.gold
FROM stats s JOIN users u on s.user_uid = u.id
ORDER BY s.overall;
Possible solution to ranking:
set #row_number=0;
SELECT (#row_number:=#row_number+1) as rank, u.username, s.overall, s.attack, s.defense, s.gold
FROM stats s JOIN users u on s.user_uid = u.id
ORDER BY s.overall;
Another, horrible looking attempt:
set #row_number = (select count(*) from users) + 1;
select (#row_number:=#row_number-1) as rank, u.username, s.overall from
stats s join users u on s.user_uid = u.id order by s.overall desc;
set #row_number = 0;
Here in PHP code, you have to run it as two queries to set the variable, then run the actual ranking query. This way, the rank variable is always set to 0 when running this. Note that I've used different table and column names, just to simplify things a little. Remember to adjust to your specific needs.
// connect to database
$conn = mysqli_connect("localhost", "user", "password", "database");
// this query will set a variable to 0.
$setSql = "SET #row_number = 0;";
// run the query. This will return a boolean - true or false, depending on whether or not the query ran successfully
$variableSet = mysqli_query($conn, $setSql);
// if the query ran successfully
if($variableSet){
// setup the actual ranking query
$statsSql = "select
(#row_number:=#row_number+1) as rank,
u.id,
u.username,
s.overall
from
mstats s
join
musers u
on
s.muser = u.id
order by
s.overall desc;";
$ranks = mysqli_query($conn, $statsSql);
if(!$ranks){
// dump error from rank query
var_dump($conn->error);
} else {
// dump results as associative array
var_dump($ranks->fetch_all(MYSQLI_ASSOC));
}
} else {
// dump errors from setting variable
var_dump($conn->error);
}
For me, the results dump looks like this:
array (size=3)
0 =>
array (size=4)
'rank' => string '1' (length=1)
'id' => string '2' (length=1)
'username' => string 'Bar' (length=3)
'overall' => string '1000' (length=4)
1 =>
array (size=4)
'rank' => string '2' (length=1)
'id' => string '6' (length=1)
'username' => string 'Tom' (length=3)
'overall' => string '7' (length=1)
2 =>
array (size=4)
'rank' => string '3' (length=1)
'id' => string '1' (length=1)
'username' => string 'Foo' (length=3)
'overall' => string '3' (length=1)
I would like to order the results of my MySQL query by their DateTime value which is under column 'timestamp' for each one.
I tried adding
$reactions = DB::query('SELECT * FROM reactions WHERE poster_id=:userid', array(':userid' => $userid));
$comments = DB::query('SELECT * FROM comments WHERE poster_id=:userid', array(':userid' => $userid));
$mentions = DB::query('SELECT * FROM comments WHERE FIND_IN_SET(:users , users)', array(':users' => $userid));
$postMentions = DB::query('SELECT * FROM posts WHERE FIND_IN_SET(:users , users)', array(':users' => $userid));
$likedMessages = DB::query('SELECT * FROM messages WHERE sender_id:senderid AND liked=:liked', array(':senderid' => $userid, ':liked' => 1));
$reports = DB::query('SELECT * FROM reports WHERE user_id=:userid AND status=:status OR status=:status2', array(':user_id' => $userid, ':status' => 1, ':status2' => 2));
But this doesn't order them at all.
Edit:
To be more clear; I would like to combine all the results from the queries then order those by timestamp
Use order by at the end of your query
ORDER BY `timestamp` DESC
I have this query:
select count(*) as total_count, sum(total) as total_value, status from invoice group by status;
when I execute it in the database I get the following:
I want to achieve the same using Doctrine2 but the following code only returns one row:
$rsm = new ResultSetMapping();
$rsm->addScalarResult('total_count', 'totalCount');
$rsm->addScalarResult('total_value', 'totalValue');
$rsm->addScalarResult('status', 'status');
$query = $this->getObjectManager()->createNativeQuery('
select count(*) as total_count, sum(total) as total_value, status from invoice group by status', $rsm);
$result = $query->getResult();
return $result;
This is the result of the above:
array (size=1)
0 =>
array (size=3)
'totalCount' => string '1432' (length=1)
'totalValue' => string '55447.80999999999' (length=2)
'status' => string 'paid' (length=4)
You should use:
$result = $query->getScalarResult();
Instead of :
$result = $query->getResult();
Hope this help
Because you are using getResult doctrine will try to fetch all matching rows for you. You need to use Doctrine's getSingleResult if you don't want the result to be an array of arrays.
So for you simply:
$result = $query->getSingleResult();
You can also limit the number of results to throw a QueryException if more or none are returned:
$query->setMaxResults(1);
My question may be not reflecting my problem, sorry for that at first. I have a problem , I need to select distinct movie names from a table also selecting its id and other records. But since ids are different for each movies, all the movies are selected(with same name ). Here's the query :
$sql = "
SELECT DISTINCT
movie_id,movie_name
FROM
tbl_current_movies as cm, tbl_movie_hall as mh
WHERE
movie_active = 'active'
AND
cm.hall_id = mh.hall_id
";
$res = $this->db->returnArrayOfObject($sql,$pgin = 'no');
var_dump($res);
And var_dump($res) says :
array
0 =>
object(stdClass)[48]
public 'movie_id' => string '1' (length=1)
public 'movie_name' => string 'MIB' (length=12)
1 =>
object(stdClass)[49]
public 'movie_id' => string '2' (length=1)
public 'movie_name' => string 'Jetuka Pator Dare' (length=17)
2 =>
object(stdClass)[50]
public 'movie_id' => string '3' (length=1)
public 'movie_name' => string 'MIB' (length=12)
So as you can see the movie MIB is showing twice, but I want to get in the results the movies MIB only once !
Change your query to this :
$sql = "SELECT movie_id , movie_name
FROM tbl_current_movies as cm
LEFT JOIN tbl_movie_hall mh ON cm.hall_id = mh.hall_id
WHERE movie_active = 'active'
GROUP BY movie_name
";
You shouldn't join your tables in the WHERE clause, if you're not confortable with the JOINs you can read some of the docs : http://dev.mysql.com/doc/refman/5.0/en/join.html
That should help you understand what they are for.
lets say we have an array like this
from a mysql function like
function getGroups($limit = 10) {
$data = $this->fetchAll ( 'SELECT gid, `group`, information, tag FROM groups
GROUP BY tag LIMIT ' . $limit );
return $data;
}
Resulting
array
0 =>
array
'gid' => string '6' (length=1)
'group' => string 'Media' (length=5)
'tag' => string 'advertising' (length=11)
1 =>
array
'gid' => string '8' (length=1)
'group' => string 'Fashion' (length=10)
'tag' => string 'shorts' (length=7)
2 =>
array
'gid' => string '7' (length=1)
'group' => string 'Automotive' (length=8)
'tag' => string 'cars' (length=5)
3 =>
array
'gid' => string '1' (length=1)
'group' => string 'Fashion' (length=7)
'tag' => string 'tshirt' (length=6)
i need to display somehow to this ( something like )
array
0 =>
array
'group' => string 'Media'
'tags'
array
0 => string 'advertising'
1 =>
array
'group' => string 'Fashion'
'tags'
array
0 => string 'short'
1 => string 'tshirt'
2 =>
array
'group' => string 'Automotive'
'tags'
array
0 => 'cars'
simpler
group tag
media advertising
fashion short
fashion tshirt
automotive cars
to
media
advertising
fashion
short
tshirt
automotive
cars
what is the best way to do this? from php array? or from the mysql ?
Are you trying to get a list of groups, plus all the tags for each group? GROUP_CONCAT() is the right way to get the tag list but you want to be grouping by group, not by tag:
function getGroups($limit = 10) {
$data = (array) $this->fetchAll (
'SELECT `group`,
GROUP_CONCAT(DISTINCT `tag` ORDER BY `tag`) AS `tags`
FROM `groups`
GROUP BY `group` LIMIT ' . $limit
);
foreach ($data as $i => $row) {
$data[$i]['tags'] = explode(',', $row['tags']);
}
return $data;
}
I would add
ORDER BY group,tag
so the result set has all rows of the same group together.
Starting a new can then be done in php by comparing to the previous group and close/starting the new group when it has changed.
actually using hashes makes more sense...
$names = array();
$query = ''SELECT gid, `group`, information, tag FROM groups GROUP BY tag LIMIT ' . $limit';
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($num_rows) {
while ($row = mysql_fetch_array($result)) {
array_push($names[$row['group']], $row['tag']);
}
}
Try this
function getGroups($limit = 10) {
$data = $this->fetchAll ( 'SELECT gid, `group`, information, tag FROM groups
GROUP BY group LIMIT ' . $limit );
return $data;
}
This will group you data with group, or you can use group_concat but then you will get short, tshirt something like this.