My question is:
I have a datebase like this:
| from | to | time | text | read |
| hans | poul | 0916 | hi there | 1 |
| john | poul | 1033 | waz up | 1 |
| hans | john | 1145 | new text | 0 |
| poul | john | 1219 | message | 0 |
| poul | hans | 1233 | respond | 0 |
I want an output where every "from" or "to" has 'hans' listed, grouped and order by time like this:
poul - 1233 - respond - 0
john - 1145 - new text - 0
Your sql query could be :-
select * from `table_name` where (`from` ='hans' or `to` ='hans') group by `from`, `to` order by time desc
Post back if you find any difficulties.
Related
I have a sql table like this,
+-------+-------+-----+
| name | start | end |
+-------+-------+-----+
| Joe | 14 | - |
| Joe | 13 | - |
| Steve | 11 | - |
| Steve | 15 | - |
| Bruce | 10 | - |
+-------+-------+-----+
and i want to update the values for column "end" with the values from "start" for each unique column value "name", so it looks like this.
+-------+-------+-----+
| name | start | end |
+-------+-------+-----+
| Joe | 14 | 14 |
| Joe | 13 | 13 |
| Steve | 11 | 11 |
| Steve | 15 | 15 |
| Bruce | 10 | 10 |
+-------+-------+-----+
my question is: how do i do this in SQL code, and how would it look like with a PHP mysql query?
This should do. Even in cases where there are multiple values.
UPDATE TableName
SET End = Start
See here -> http://sqlfiddle.com/#!9/465c22/1
I have quiet interesting task at my university.
I have DB table:
----------------------------------------
| id | fee | status | created_at |
----------------------------------------
| 1 | 10 | COMPLAINABLE | 2018-05-01 |
| 2 | 15 | COMPLAINABLE | 2018-05-01 |
| 3 | 18 | COMPLAINABLE | 2018-05-02 |
| 4 | 1 | COMPLAINABLE | 2018-05-03 |
| 5 | 2 | COMPLAINABLE | 2018-05-03 |
----------------------------------------
I wrote SQL:
SELECT created_at AS ts, SUM(fee) AS value
FROM leads_ads
WHERE status NOT IN ('COMPLAINED', 'COMPLAIN_ACCEPTED')
GROUP BY DATE_FORMAT(created_at, '%Y-%m')
ORDER BY created_at ASC;
And at first result was fine:
-------------------------
| ts | value |
-------------------------
| 2018-05-01 | 25 |
| 2018-05-02 | 18 |
| 2018-05-03 | 3 |
-------------------------
But now i have to rewrite sql to get result like this:
---------------------------------------------------------------
| ts0 |value0| ts1 | value1 | ...... | tsn | valuen |
---------------------------------------------------------------
|2018-05-01| 25 |2018-05-02| 18 | ...... | tsn | valuen |
---------------------------------------------------------------
I tried many ways to do it but can't fine a right result(tried TRANSPOSE pivot table). So I decided to ask here. maybe someone will push me to the right way.
Thanks
Look at this article (this shows how you can resolve this with dynamic SQL): http://onlybluefeet.com/2015/01/18/how-to-rotate-rows-into-columns-in-mysql/
I am trying to count how many require position there are for each jobseeker.
I have two tables: jobseeker and jobposition.
jobseeker:
+----+---------+-----------+----------------+
| id | fb_name | fullname | desireposition |
+----+---------+-----------+----------------+
| 1 | John | John Cena | 3 |
| 2 | Christ | Christ | 4 |
| 3 | Thomas | Cfitcher | 2 |
+----+---------+-----------+----------------+
and jobposition:
+----+--------+------------------+
| id | job_id | require_position |
+----+--------+------------------+
| 1 | 12 | 3 |
| 2 | 13 | 3 |
| 3 | 14 | 4 |
| 4 | 15 | 5 |
| 5 | 16 | 4 |
| 6 | 17 | 3 |
+----+--------+------------------+
My expected result is:
+----+---------+-----------+----------------+-----------------------+
| id | fb_name | fullname | desireposition | total_requireposition |
+----+---------+-----------+----------------+-----------------------+
| 1 | John | John Cena | 3 | 3 |
| 2 | Christ | Christ | 4 | 2 |
| 3 | Thomas | Cfitcher | 2 | 0 |
+----+---------+-----------+----------------+-----------------------+
I want to count how many require position there for each jobseeker.
Here is what I tried using crossJoin, but am unsure which join I actually need to be using.
$jobseekers = Jobseeker::crossJoin('jobpositions')
>select('fullname','fb_name','desire_position', DB::raw('count(require_position) as total_requireposition'))
->groupBy('fullname')->paginate(10);
Can anyone help guide me? Any help would be highly appreciated.
The regular MySQL query you want is:
SELECT s.id, fullname, fb_name, desireposition, IFNULL(COUNT(require_position), 0) AS require_position
FROM jobseeker AS s
LEFT JOIN jobposition AS p ON s.desireposition = p.require_position
GROUP BY s.id
I don't use Laravel, but I think the translation would be:
$Jobseeker->select('fullname','fb_name','desire_position', DB::raw('IFNULL(COUNT(require_position), 0) as total_requireposition'))
->leftjoin('jobposition', 'desireposition', '=', 'require_position')
->groupBy('jobseeker.id')
->paginate(10)
I have a table of words used in the title of articles. I want to find which words which are used the least in the set or article titles.
Example:
Titles:
"Congressman Joey of Texas does not sign bill C1234."
"The pretty blue bird flies at night in Texas."
"Congressman Bob of Arizona is the signs bill C1234."
The table would contain the following.
Table WORDS_LIST
----------------------------------------------------
| INDEX ID | WORD | ARTICLE ID |
----------------------------------------------------
| 1 | CONGRESSMAN | 1234 |
| 2 | JOEY | 1234 |
| 3 | SIGN | 1234 |
| 4 | BILL | 1234 |
| 5 | C1234 | 1234 |
| 6 | TEXAS | 1234 |
| 7 | PRETTY | 1235 |
| 8 | BLUE | 1245 |
| 9 | BIRD | 1245 |
| 10 | FLIES | 1245 |
| 11 | NIGHT | 1245 |
| 12 | TEXAS | 1245 |
| 13 | CONGRESSMAN | 1246 |
| 14 | BOB | 1246 |
| 15 | ARIZONA | 1246 |
| 16 | SIGNS | 1246 |
| 17 | BILL | 1246 |
| 18 | C1234 | 1246 |
----------------------------------------------------
In this case, the words "pretty,blue, flies, night" would be the used in the least number of articles.
I would appreciate any ideas on how to best create this query. So far below is what I started with. I can also write something in PHP but figured a query would be faster.
SELECT distinct a1.`word`, count(a1.`word`)
FROM mmdb.words_list a1
JOIN mmdb.words_list b1
ON a1.id = b1.id AND
upper(a1.word) = upper(b1.word)
where date(a1.`publish_date`) = '2017-06-09'
group by `word`
order by count(a1.`word`);
I don't see why a self-join is necessary. Just do something like this:
select wl.word, count(*)
from mmdb.words_list wl
where date(wl.`publish_date`) = '2017-06-09'
group by wl.word
order by count(*);
You can add a limit to get a fixed number of words. If publish_date is already a date, you should do the comparison as:
where publish_date = '2017-06-09'
If it has a time component:
where publish_date >= '2017-06-09' and publish_date < '2017-06-10'
This expression allows MySQL to use an index.
Try this. It's a bit more simple and should return the correct results:
SELECT `WORD`,
COUNT(*) as `num_articles`
FROM `WORDS_LIST`
WHERE date(`publish_date`) = '2017-06-09'
GROUP BY `WORD`
ORDER BY COUNT(*) ASC;
MySQL Database Table: (Name: Attendance)
RecID | EmployeeID | Date | Name | WorkedHours | Absence | OverTime
------+------------+----------+------+-------------+---------+---------
434 | 7 | 19/11/16 | Jack | 8 | | 1
435 | 7 | 20/11/16 | Jack | 8 | | 0
436 | 7 | 21/11/16 | Jack | 8 | | 0
437 | 8 | 19/11/16 | Nik | 8 | | 1
438 | 8 | 20/11/16 | Nik | 0 | 1 | 0
439 | 8 | 21/11/16 | Nik | 8 | | 1
I would like to show the following payroll report using PHP:
EmployeeID | Month | Name | WorkedDays| Deductions| Overtime | Net Pay
-----------+-------+------+-----------+-----------+----------+--------
7 | 11/16 | Jack | 3 | | | 1
8 | 11/16 | Nik | 2 | | | 0
What I tried so far was the following using SQL:
SELECT EmployeeID, Name,
COUNT(*) AS WorkedDays,
(COUNT(*) * $DailyWage) AS 'Net Pay'
FROM `Attendance`
GROUP BY EmployeeID;
Is there a way I can do that through a PHP loop? Something like
$SQL = mysql_query("SELECT * FROM Attendance GROUP BY EmployeeID");
while($Row = mysql_fetch_array($SQL)){
$WorkedDays++;
echo "WorkedDays: " . $WorkedDays;
// Doesn't make sense but just giving example.
}
Please let me know if you need any details. Thank you.