I have table, named "table_log".
Here is the structure
---------------------------------------
|id_log | user_id | login_date |
---------------------------------------
|1 | 1 |2014-09-02 14:58:53 |
|2 | 1 |2014-09-03 24:18:53 |
|3 | 1 |2014-09-02 14:58:53 |
|4 | 1 |2014-09-01 02:28:53 |
|5 | 2 |2014-09-04 01:48:53 |
|6 | 3 |2014-09-05 04:58:53 |
|7 | 2 |2014-09-06 03:58:53 |
----------------------------------------
I want to count number of user each days. not how much log is.
As an example data, I want to show it like this:
---------------------------
|date | user_number|
---------------------------
|2014-09-02 | 1 |
|2014-09-03 | 1 |
|2014-09-04 | 5 |
---------------------------
Does any can help me? How to query my database?
SELECT date(login_date) AS date, count(DISTINCT user_id) AS user_count
FROM table_log
GROUP BY date(login_date)
The date function gives just the date-part of a datetime column, then it's a simple group by.
I dont get your ques, but u expect it?
2014-09-03 there is one record how user number will come 4 ? which scenario u asking?
SELECT date(login_date) AS date, count(date(login_date))
FROM tbl
GROUP BY date(login_date)
Related
I have a MySQL table that looks like this:
id | id_item |
1 | T0001 |
2 | T0002 |
2 | T0003 |
3 | T0004 |
and I want to change it like this:
id | id_item |
1 | T0001,T0002 |
2 | T0001,T0003 |
2 | T0001,T0004 |
3 | T0002,T0003 |
4 | T0002,T0004 |
5 | T0003,T0004 |
and I want save in table item_2 like this:
id | id_item |
1 | T0001 |
2 | T0002 |
3 | T0001 |
4 | T0003 |
5 | T0001 |
6 | T0004 |
7 | T0002 |
8 | T0003 |
9 | T0002 |
10 | T0004 |
11 | T0003 |
12 | T0004 |
Does anyone know how to do this by using PHP or MySQL?
You can generate this output via the following query:
SELECT
CONCAT(t1.id_item, ',', t2.id_item) AS id_item
FROM yourTable t1
INNER JOIN yourTable t2
WHERE
t2.id > t1.id
ORDER BY
t1.id,
t2.id
If you wanted to insert this data, you can use the above select to do an INSERT INTO ... SELECT into a new table. I mention new table, because it probably would not make much sense to store CSV and non CSV data in the same column. Actually, I don't at all recommend storing CSV data in the first place. Instead, just generate it if you need it in your presentation layer using a query similar to what I have given above.
Demo here:
Rextester
I am doing a script want to calculate how many row record before an user record when t1.status is 1.
My table is t1, and the data as below:
+------+---------+------------+----------+----------+
| ID | name | desc | status | time |
+------+---------+------------+----------+----------+
| 1 | ABB | | 1 | 0325 |
| 2 | CCD | | 1 | 0236 |
| 3 | EEF | | 1 | 0325 |
| 4 | GGG | | 1 | 0000 |
| 5 | HIJ | | 2 | 1234 |
| 6 | KKK | | 1 | 5151 |
+---------------------------------------------------+
I was thinking about the query is something like (query row where status = 1 AND stop when reach $userid)
I would like to output to show user (Let's say username is GGG) as:
$userid = 'GGG';
then my output will be
<table><tr><td>Queue: GGG You came in 4 place, in front of you still got 3 person in queue, please be patient</td></tr></table>
How to I do the right query to get the number 4 and 3 ?
Thank you.
You can try something like this hope it helps :-
SELECT count(*) as COUNT FROM t1 WHERE id < (SELECT id FROM t1 WHERE userid = $userid)
I'm trying to build a query using data from 4 tables: Bookings, Users, Events, Locations
Bookings :
+---------------------------------+
|book_id | event_id | person_id |
+---------------------------------+
|1 | 1 | 2 |
|2 | 2 | 1 |
|3 | 2 | 2 |
|4 | 1 | 3 |
|5 | 3 | 1 |
|6 | 3 | 2 |
+---------------------------------+
Users :
+----------------------+
| user_id | name |
+----------------------+
| 1 | Joe |
| 2 | Jack |
| 3 | Jane |
+----------------------+
Events :
+------------------------+
| event_id | location_id |
+------------------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
+------------------------+
Locations :
+---------------------------+
| location_id | name |
+---------------------------+
| 1 | Lombard |
| 2 | NYC |
| 3 | LA |
+---------------------------+
The query that I can't seem to write should get me to display a table like this :
+------------------------------+
+Name |Lombard|NYC|LA|Total|
+------------------------------+
+Joe |1 |0 |1 |2 |
+Jack |2 |0 |1 |3 |
+Jane |1 |0 |0 |1 |
+------------------------------+
+Totals |4 |0 |2 |6 |
+------------------------------+
What I got to work is displaying how many booking have been made per user but not per user AND per location using this query:
$query='
SELECT
bookings.person_id,
COUNT(bookings.person_id) AS total,
bookings.event_id,
users.display_name
FROM bookings
INNER JOIN users ON bookings.person_id=users.id
WHERE users.id=bookings.person_id
GROUP BY bookings.person_id';
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_array($result))
{
/* total bookings per user */
$value = $row['total'];
$sum += $value;
/* Displaying results */
echo "<tr width='500'>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['display_name']."</td>";
echo "<td>".$row['total']."</td>";
echo "</tr>";
}
This works okay and displays :
+-----------------------------------+
| ID | NAME | Total Bookings |
+-----------------------------------+
| 7 | Bob | 3 |
| 5 | Jane | 2 |
| 3 | Joe | 1 |
+-----------------------------------+
Could you please help me getting there. Thanks.
You are looking for a pivot table style query. Here's one way you can do it.
select u.name,
count(case when l.name = 'Lombard' then 1 end) as lombard,
count(case when l.name = 'NYC' then 1 end) as nyc,
count(case when l.name = 'LA' then 1 end) la,
count(u.name) total
from bookings b
inner join events e
on b.event_id = e.event_id
inner join locations l
on e.location_id = l.location_id
inner join users u
on u.user_id = b.person_id
group by u.name
with rollup
fiddle here
It gets a lot harder (and is generally easier to do in the application) if you dont know the possible column (location) values when you are writing the query.
http://sqlfiddle.com/#!9/92d50
SELECT u.name,
SUM(l.name = 'Lombard') lombard,
SUM(l.name = 'NYC') nyc,
SUM(l.name = 'LA') la,
COUNT(*) total
FROM bookings b
LEFT JOIN `events` e
ON b.event_id = e.event_id
LEFT JOIN locations l
ON e.location_id = l.location_id
LEFT JOIN users u
ON u.user_id = b.person_id
GROUP BY u.name
WITH ROLLUP
I have two table 'users' and 'friends' I am having difficulty joining them
users table
id | name | usercode
--------------------
1 | david | 2WM
2 | Samme | E5N
3 | Awudu | C0Q
4 | John | VX6
5 | Jerem | FG3
Friends Table
id | actor | target
--------------------
1 | E5N | FG3
2 | 2WM | VX6
3 | FG3 | 2WM
4 | C0Q | VX6
5 | FG3 | VX6
Basically i want to select all users from USERS table who has 'FG3' in either target or actor column in the FRIENDS table.
The result will be
id | name | usercode | actor | target
--------------------------------------
2 | Samme | E5N | E5N | FG3
1 | david | 2WM | FG3 | 2WM
5 | John | VX6 | FG3 | VX6
I have triend everything i know but still i am not getting the correct results
I will be glad if anyone can help me since I need to present this work tomorrow morning. Thank you
Looks like you want to join on usercode equals actor or target, then put the 'FG3' part in a WHERE clause:
SELECT users.id, users.name, users.usercode, friends.actor, friends.target
FROM users
INNER JOIN friends
ON users.usercode = friends.actor OR users.usercode = friends.target
WHERE users.usercode != 'FG3'
AND (friends.actor = 'FG3' OR friends.target = 'FG3');
Using INNER JOIN limits your query to only records that exist in both tables.
I am trying to get some statistics for an online game I maintain. I am searching for an SQL statement to get the result on the bottom.
There are three tables:
A table with teams, each having a unique identifier.
table teams
---------------------
| teamid | teamname |
|--------|----------|
| 1 | team_a |
| 2 | team_x |
---------------------
A table with players, each having a unique identifier and optionally an affiliation to one team by it's unique teamid.
table players
--------------------------------
| playerid | teamid | username |
|----------|--------|----------|
| 1 | 1 | user_a |
| 2 | | user_b |
| 3 | 2 | user_c |
| 4 | 2 | user_d |
| 5 | 1 | user_e |
--------------------------------
Finally a table with events. The event (duration in seconds) is related to one of the players through their playerid.
table events.
-----------------------
| playerid | duration |
|----------|----------|
| 1 | 2 |
| 2 | 5 |
| 3 | 3 |
| 4 | 8 |
| 5 | 12 |
| 3 | 4 |
-----------------------
I am trying to get a result where the durations of all team members is summed up.
result
--------------------------
| teamid | SUM(duration) |
|--------|---------------|
| 1 | 14 | (2+12)
| 2 | 15 | (3+8+4)
--------------------------
I tried several combinations of UNION, WHERE IN, JOIN and GROUP but could not get it right. I am using PostgreSQL and PHP. Can anyone help me?
Just use sum with group by:
select t.teamid, sum(e.duration)
from team t
join players p on t.teamid = p.teamid
join events e on p.playerid = e.playerid
group by t.teamid
If you need all teams to be returned even if they don't have events, then use an outer join instead.
Try this
SELECT teamid, Sum(duration),
AS LineItemAmount, AccountDescription
FROM teams
JOIN teams ON teams.teamid = players.teamid
JOIN events ON players.playersid = events.playersid
JOIN GLAccounts ON InvoiceLineItems.AccountNo = GLAccounts.AccountNo
GROUP BY teamid
http://www.w3computing.com/sqlserver/inner-joins-join-two-tables/