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

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.

Related

Create result from double table query with missing record

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

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/

Most efficient way to get the data from database

So what i have is pretty simple.
I have 1 table
CategorieMain
like
CatMainid
CatMainName
and 1 table:
CategorieSub
like
CatId
CatName
CatMainId
'
What i want is :
A list with all the CatMainName's and under the names all the CatSubNames.
CatMainId1
all CatSubNames with CatMainId1
CatMainId2
all CatSubNames with CatMainId2
Etc Etc
Currently i use php to get the data like
SELECT * from Categoriemain
and in the while loop i do
SELECT * FROM CategorieSub WHERE CatMain id = $row['CatMainId']
But this is very inefficient because now if i have 10 CatMainId's i do 10 query's (for each one a while)
What is the most efficient way to get a list like this, i was thinking about putting it in arrays or something but i couldn't get it working?
SELECT * FROM Categoriemain
JOIN CategorieSub ON Categoriemain.CatMainid=CategorieSub.Catmainid
ORDER BY Categoriemain.CatMainid
Use one query:
SELECT Categoriemain.*, CategorieSub.CatSubNames FROM Categoriemain
JOIN CategorieSub ON Categoriemain.CatMainid=CategorieSub.Catmainid
Edited, removed GROUP BY
With this query, you don't need the while loop.
I have also added the CatSubNames to the output list of the query fields.

SQL join into array for each table

I am joining two tables: customers and queries.
I am getting the full_name from the customers table and the description from the queries table.
I am wondering if it is possible to have the results of an SQL join split into arrays that correspond with the table the data came from? For example:
$STH = $DBH->prepare("SELECT queries.description, customers.full_name FROM queries INNER JOIN customers ON queries.customer_id = customers.id");
$STH->execute();
$queries = $STH->fetchAll();
At the moment, I can access my data like this: $queries[0]['description'] and $queries[0]['full_name']
However, my question is whether there is an easy way to get the data like so: $job[0]['query']['description'] and $job[0]['customer']['full_name'].
Just as teresko mentioned, I can't understand why you'd need that.
I can only imagine you want to see on the PHP code what are the table that contained the information.
Maybe you could do something like SELECT queries.description as queries_description, then your php code would look like $queries['queries_description']. Would it be enough?
You can loop through the results in PHP and convert it to the data structure you want, but you cannot (as far as I know) automatically group the data into arrays based on the source table. A (somewhat messy) alternative, using SQL is to use a multi-query and create a temp table from your original results, then select the results on a per-table basis, like so:
CREATE TEMPORARY TABLE q AS
(SELECT queries.description, customers.full_name FROM queries
INNER JOIN customers
ON queries.customer_id = customers.id
);
SELECT q.description FROM q;
SELECT q.full_name FROM q;
So, in those SELECT statements, you'll have to list all the columns that you want for each result. Then in PHP, you'll have to iterate over each resultset and put the data into arrays (or objects/whatever) as needed. Errr. A fetchAll will still not get you what you want, but a fetchAll on the first non-empty resultset will get you all the rows from queries and the 2nd will get you all the rows from customers

PHP MYSQL Query Multiple Tables Where the Second Table returns multiple rows

I'm trying to query 2 tables where the first table will return 1 row and the second table will return multiple rows. So basically the first table with return text on a page and the second table will return a list that will go within the page. Both tables have a reference row which is what both tables are queried on. (See Below)
SELECT shop_rigs.*, shop_rigs_images.*, shop_rigs_parts.*
FROM shop_rigs
LEFT JOIN shop_rigs_images
ON shop_rigs.shoprigs_ref = shop_rigs_images.shoprigsimg_ref
LEFT JOIN shop_rigs_parts
ON shop_rigs.shoprigs_ref = shop_rigs_parts.shoprigsparts_ref
WHERE shoprigs_enabled='1' AND shoprigs_ref='$rig_select'
ORDER BY shoprigs_order ASC
Is it better to just do 2 queries?
Thanks,
dane
I would do this in two queries. The problem isn't efficiency or the size of the respective tables, the problem is that you're create a Cartesian product between shop_rigs_images and shop_rigs_parts.
Meaning that if a given row of shop_rigs has three images and four parts, you'll get back 3x4 = 12 rows for that single shop_rig.
So here's how I'd write it:
SELECT ...
FROM shop_rigs
INNER JOIN shop_rigs_images
ON shop_rigs.shoprigs_ref = shop_rigs_images.shoprigsimg_ref
WHERE shoprigs_enabled='1' AND shoprigs_ref='$rig_select'
ORDER BY shoprigs_order ASC
SELECT ...
FROM shop_rigs
INNER JOIN shop_rigs_parts
ON shop_rigs.shoprigs_ref = shop_rigs_parts.shoprigsparts_ref
WHERE shoprigs_enabled='1' AND shoprigs_ref='$rig_select'
ORDER BY shoprigs_order ASC
I left the select-list of columns out, because I agree with #Doug Kress that you should select only the columns you need from a given query, not all columns with *.
If you're pulling a large amount of data from the first table, then it would be better to do two queries.
Also, for efficiency, it would be better to specify each column that you actually need, instead of all columns - that way, less data will be fetched and retrieved.
Joins are usually more efficient than running 2 queries, as long as you are joining on indexes, but then it depends on your data and indexes.
You may want to run a "explain SELECT ....." for both options and compare "possible keys" and "rows" from your results.

Categories