I realy need help to make this work coz i dont know how sorting works and i am begginer in this field guys.
I have table like this:
jobs - hours - user
I am trying to sort and output table like this:
Find all jobs that exist in table horizontal
and then, in vertical menu show all users with hours (if exist) on all jobs add 0 if not
joob 1 - joob2 - joob 3 - joob 4
mark 0 3 1 0
benny 7 5 0 0
john 0 0 0 3
How do i query and sort like this output as html ?
I know latter to group and calculate but this i am having problem with.
Related
I have a database of items and each item has various number of properties. Is it possible for MySql only to return items that have a certain number of matches (not properties) when a search is run?
Example: I am searching for any item with a wheel that is red and has a tire.
This would return all items with these three matches even if they have more properties and would automatically exclude anything that has less than 3 matches.
I have tried playing with the COUNT + GROUP BY + HAVING but I was unable to put together a meaningful working code. Before I spend more time on this I would like to know if it is possible at all.
TABLE DESIGN
ID ITEM PROPERTY
1 1 red
2 1 wheel
3 1 tire
4 2 red
5 2 wheel
6 2 tire
7 2 lamp
8 3 red
9 3 wheel
10 4 red
I would like it to return ITEM 1 and 2
You would do this with a group by and having. You really provide no information about your data structure, but the basic idea is:
select ip.item
from design ip
where ip.property in ('wheel', 'red', 'tire')
group by ip.item
having count(distinct ip.property) = 3;
I am planning to code a project status monitor, the idea in matrix is like below
require1 require2 require3
task1 1 2 0
task2 1 0 2
task3 0 1 1
task4 1 0 0
eg. when there is a new project, task1,2,3... should be included, and the requirement 1,2,3... is need to complete for the correspondent task, the letter 0,1,2 to represent that if 1 record in require1 and 2 in require2 and 0 in require3 means the task1 is completed.
my question is that, should I put this matrix into a database then using it to join the record table (concerning the speed if in mass of record), or can I use an array to implement in simply way ?
Please help to suggest and let me know how to use array!
Thank you.
I'm generally pretty self reliant on fudging something together that works but I have run into a brick wall on this one and am eventually reaching out for a nod in teh right direction..
my query:
$post_views = (int)$wpdb->get_var("
SELECT SUM(count) AS views
FROM ".$wpdb->prefix."post_views
WHERE id IN (".$post_id.") AND type = 0"
The database table looks like this :
id type period count
------- ------- ----------- -------
32310 0 20141023 8
32310 0 20141022 68
32310 1 201443 76
32310 2 201410 76
32310 3 2014 76
32310 4 total 76
The type 0 are the ones I'm interested in, I just want the sum of the COUNT column for the most recent 7 type 0 entries
I have been trying with things based around "ORDER BY period DESC LIMIT 7 " on the end of the query - to no avail, I generally get returns of 0 doing this.
a new type 0 row will be generated for each article every day, so thats why I need to only get the last 7
any help here would be massively appreciated, totally stuck for the first time ever with this.
SELECT SUM(count)
FROM (SELECT count
FROM wp_post_views
WHERE type = 0
AND id IN (684,42,7)
ORDER BY period DESC
LIMIT 7)
Or just determine the date a week ago first and use that to filter, but a subquery like this will work fine as well.
Given a table like the one below, what would be the best way to detect the two columns separately?
So what I would need the total colspans for the first column.
What is important to remember is that the nr of columns can change.
In the case of this example, the second column starts at "10 euro" (second row). The first section is equal to 2 colspans. The other section is 5 colspans.
Any (abstract) ideas on how to do this?
You must consider the gaps in between the table cells and mark their positions, like this::
0 1 2 3 4 7
0 2 3 4 5 6 7
0 1 2 4 5 7
...
0 2 7
Once you have built an array with above information, you iterate over them and mark the common gap locations:
0 2 7
Since 0 and 7 are both at the edges of your table, you can strip those off. Then you're left with position 2 as the common gap between your rows.
Done :)
I might be doing this the wrong way when I set up the tables?
I'm using mysql & php. Loads of googleing only shows how to count how many times a value appears in several rows, I want check for a value in many different columns but in the same record. (I think this: count number of columns that have data for each row just might be about the same thing, maybe? but I don't get it.)
I have a table with goals that I hope to work on and achieve every day. So for every day I wish to mark: "success" or "fail". And insert is working great. How ever I'm looking for a way to calculate the number of success& number of fails of "today", to show the right kind of smiley which will be encouraging or sad depending on number of fails and number of successes.
For example:
ID date drinkMoreWater goToBedEarlier callADearFriend
1 2012 jan 15 fail fail fail
1 2012 jan 16 success _(still empty) success
So if today is jan 15 the smiley will be very very sad.
If today is jan 16 the smiley will be really really hppy with stars in it's eye's (atleast until I fail goal 2 ;) )
Your tables should actually be structured differently to make your life easier:
Goals:
ID Goal
1 drinkMoreWater
2 goToBedEarly
3 callADearFriend
Status:
ID Status
1 Success
2 Fail
Tracking:
ID Date Goal_ID Status_ID
1 1/1/2012 1 1
1 1/1/2012 3 2
1 1/2/2012 2 1
1 1/2/2012 4 1
Now you could easily add goals and status (e.g. 'Working on it') and your table structure does not have to change to accommodate your changes, your queries become a lot simpler as well.
If you want to do that in SQL, you could use a ternary-like construct, like that:
SELECT *,
(CASE drinkMoreWater WHEN 'success' THEN 1 ELSE 0 END)
+ (CASE goToBedEarlier WHEN 'success' THEN 1 ELSE 0 END)
+ (CASE callADearFriend WHEN 'success' THEN 1 ELSE 0 END)
AS numberOfSuccesses
FROM yourTable
You can do something like this:
Select case when sub.smilies = 0 then "very very sad" else
case when sub.smilies = 1 then "sad" else "hubby" end
end as "Number of Smilies"
from
(
Select case when t.drinkMoreWater = "fail" then 0 else 1 end +
case when t.goToBedEarlier = "fail" then 0 else 1 end +
case when t.callADearFriend = "fail" then 0 else 1 end as smilies
from yourTableName t
where date = #date
) sub
You will need to handle the empty strings, and work around these case statements.
This solution is for your current design but you better off consider the redesign suggested by #BassamMehanni 's answer