Create result from double table query with missing record - php

The two tables are basically identical with a user id, date and count.
I need to get results for both tables via user id, but match the dates. If a date is missing from either table (ie it is in one table but not the other) I need to assign a 0 for "count" for that returned result. Can this be done in the query or do I need to do this in PHP as well?
Thanks for any suggestions!

I ended up building an array for both table results, then picking the bigger array via count and running a loop through it comparing keys to the smaller array. Thanks everyone for their insight!

You can select from both tables with inner join, and after that you can add condition with WHERE statement, like WHERE date != null, or date != ''.
If you'd like to read more about inner join, this is your site below:
https://www.w3schools.com/sql/sql_join_inner.asp

Related

PHP Mysql count distinct reasons given where reason value in another table

I want to create a statistics page where i can count the number of reasons given in a table ...The reasons are created dynamically and stored in another table.
Table1 contains all the reasons which populate a drop down box...
Table2 contains the entries given by the user one of the columns is the reason value.
I would like to count the amount of times each reason appears .
Thankyou for your time and help
Looks like simply group statement:
select
reason.name reason,
count(*) count
from entry, reason
where
entry.reason = reason.id and
entry.time > now() - interval 7 day -- you probably need some filter
group by reason.id

how to get num row where object by other table use codeigniter

I have 2 tables. Consider tb_burung and tb_data.
This is the structure of tb_burung:
This is the structure of tb_data:
I want to get the number of rows of tb_data order id_burung for viewing on a page like this:
Not sure what exactly you want to achieve, but just try following sql:)
select t1.burung, count(t2.id_burung) as burungCnt
from tb_burung t1
left join tb_data t2 on t1.id_burung = t2.id_burung
group by t1.id_burung, t1.burung
First of all retrieve data from both tables individually. And then loop tb_burung table array and use array_filter out data from tb_data table array according to tb_burung id, use count function to get count of the filtered data. By doing like this, the process will be.
Two database query
3 loop, 3 array_filter and 3 count
u got the count.

Mysql intersect two strings

I have the following tables:
TableFinal
column id, with first row having value 1
column numbers, with first row having value `1,5,6,33,2,12,3,4,9,13,26,41,59,61,10,7,28`
And
TablePick
column id, with first row having value 1
column numbers, with first row having value 2,12,26,33
I want to check if the numbers from TablePick, column "selected" are contained in the column "numbers" of TableFinal.
I have to mention that in TablePick, the numbers in column "selected" are ordered ASC, while in TableFinal, the numbers in column "numbers" are shuffled.
Usually I would put each of these in an array using PHP and then intersect the 2 arrays and count the resulted array. But in MYSQL, it is not that simple, so practically I have no idea where to start.
Maybe I should create an ARRAY_INTERSECT function? Or do we have a simpler solution?
SELECT * FROM TablePick p RIGHT JOIN TableFinal f ON f.id=p.id WHERE ARRAY_INTERSECT(p.selected,f.numbers)
Sorry to say so, but your schema needs some serious maintenance: NEVER EVER store more than one information in one field, if you need to access them separately.
You need a pair of join tables, where instead of the first row (1, "1,5,6,33,2,12,3,4,9,13,26,41,59,61,10,7,28") you have the rows
(1,1)
(1,5)
(1,6)
(1,33)
...
and instead of the row (1, "2,12,26,33") you have the rows
(1,2)
(1,12)
(1,26)
(1,33)
Now you query is simply:
SELECT ... FROM TableFinal
INNER JOIN TABLE TablePick ON TableFinal.number=TablePick.number
WHERE TableFinal.id=1
AND TablePick.id=1
EDIT
Please understand, that even if this were possible without MySQL abuse, it would be a performance killer, once the number of rows start to rise: We are talking of n*m array intersects, if the tables have n and m rows respectivly.

Fetching few records from mysql database having million entries

I have a table which has million rows. It has user id as its primary key. I have an array having 500 user ids in it.
I want to select all the records from the table whose user ids are in the array. I know one method to do this is to change the array into a string and run IN query by passing the string.
But I think it is not the efficient way to do it. So kindly suggest other ways.
I am assuming that your ids are integer. Maybe you are getting this list of Ids from some other sources so that a join on mysql side is not desired solution. If yes, then find the maximum and minimum id present in your 500 Ids list. You can do this in php side. When you have the max and min value, then query mysql db with a where clause
select ...
from table_name
where min_id <= id and id <= max_id
id is the primary key so the advantage is that it is already indexed.
I have done this in the past, I am not sure that my method is the most efficient.
I create a string out of the ids: where id = a or id = b or id = c ...
then I add the select statement in front of it, and do a fetchall.
My guess is that you're getting these user IDs from another table and that you are storing them in an array. If this is correct, then you should change the query that fetches these user IDs so that it uses a join instead.
Joins will help you there, because IN() is not a good programming practice.
You can learn about joins here : http://mysqljoin.com/

How do I retrieve data with MySQL such that I won't be getting duplicate values in a single column?

I am currently working on a school system where we have a parent course and a child course (meta_courses in Moodle).
So, we have a table mdl_course_meta and it has 3 fields. Id, parent_course and child_course.
My problem is that a parent course can have many child courses so that means, for example, a parent_course = 50 can appear two times in the table which means it has 2 child courses. I just want to be able to find all the parent courses without it returning the same value twice or more times. I'm currently using this query right now which obviously doesn't do what I want:
$q = "SELECT * FROM mdl_course_meta";
I am working with PHP as well by the way.
Thanks a lot.
SELECT DISTINCT parent_course from mdl_course_meta
That should do it if you just want the course names. One thing to keep in mind, if you want other fields this is not going to work the way you want it to(how would it know which record to choose if there are multiple records with the same parent_course and you only want one).
This approach can only be used if you only want to return the parent_courses without duplicates.
DISTINCT helps to eliminate duplicates. If a query returns a result that contains duplicate rows, you can remove duplicates to produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list.
$q = "SELECT DISTINCT parent_course FROM mdl_course_meta";
If you don't want duplicate values in a single column, use GROUP BY parent_course.
In this way you are free to select any column.
If you only want distinct values for a particular column column, then you can use GROUP BY:
SELECT *
FROM mdl_course_meta
GROUP BY parent_course
The values in the other columns will be arbitrary. This will work in MySQL 5.x.
MySQL 4.x won't let you be arbitrary, so you can't mix aggregate and non-aggregate columns. Instead, you'd have to do something like this, which gets a bit complicated:
SELECT MAX(col1), MAX(col2), parent_course, MAX(col4), ...
FROM mdl_course_meta
GROUP BY parent_course
This way, the values aren't arbitrary. You've specified the ones you want.

Categories