How can I do JOIN in codeigniter? Im just a newbie in programming using codeigniter.
I have 2 tables an item_db table and a mob_db table.
item_db table:
+-----+---------------+
| id | name |
+-----+---------------+
| 501 | Red Potion |
+-----+---------------+
| 502 | Orange Potion |
+-----+---------------+
| 503 | Green Potion |
+-----+---------------+
mob_db table:
+------+---------+----------+---------+----------+
| ID | Drop1id | Drop1per | Drop2id | Drop2per |
+------+---------+----------+---------+----------+
| 1001 | 501 | 7000 | | |
+------+---------+----------+---------+----------+
| 1002 | 502 | 500 | 503 | 2500 |
+------+---------+----------+---------+----------+
| 1003 | 501 | 7000 | 502 | 500 |
+------+---------+----------+---------+----------+
how can I change the variable into the name that is match in item_db?
If I got the variable 501 from mob_db, how can I change it into a name that match in item_db table as well as the percentage which is the Drop1per or Drop2per or Drop3per?
Example:
501 - 1000%
502 - 500%
503 - 2500%
Result must be:
Red Potion - 10%
Orage Potion - 5%
Green Potion - 25%
I hope you understood my issue. Thanks
Related
There are two measures in one fact table \ dimension. Measure 'YearTotal' should somehow be pre-calcuated as a distinct value for any futher summing (aggregating). And 'YearTotal' can't be derived from 'YearDetail' measure, so they are completely independent.
+-------------+---------------+-----------+------------+
| AccountingID | Date | TotalYear | YearDetail |
+--------------+---------------+-----------+------------+
| account1 | 31.12.2012 | 500 | 7 |
| account1 | 31.12.2012 | 500 | 3 |
| account1 | 31.12.2012 | 500 | 1 |
| account2 | 31.12.2012 | 900 | 53 |
| account2 | 31.12.2012 | 900 | 4 |
| account2 | 31.12.2012 | 900 | 9 |
| account3 | 31.12.2012 | 203 | 25 |
| account3 | 31.12.2012 | 203 | 11 |
| account3 | 31.12.2012 | 203 | 17 |
+--------------+---------------+-----------+------------+
So, the question: What should be in (pre)calculated measure expression to get such a result:
select
(
[Accounting Dim].[Account ID]
[Dim Date].[Calendar Year].&[2012]
) ON COLUMNS
from [Cube]
WHERE [Measures].[YearTotal]
in case of correct expression the answer would be --> (500+900+203) = 1603
(and optionaly): maybe there is a common distinct pattern solution for any other simple types of aggregation
Maybe go with MAX at the column level [TotalYear] and enforce a specific level of calculation.
CREATE MEMBER [Measures].[YearTotal] AS
MAX(
(
[Calendar Dim].[Year].[All].Children,
[Accounting Dim].[Account ID].[All].Children
),
[Measures].[TotalYear]
)
This is my product table.I want to store customer_id from 1000 and save by +1 how much data i stored
id | customer_id | name |
1 | 1000 | ABC |
2 | 1001 | Tripathi |
3 | 1002 | Leaptrig |
4 | 1003 | Falcon |
5 | 1004 | Savillan |
6 | 1005 | Molt |
7 | 1006 | Falt |
My Controller
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=1000+($lastProduct+1);
}
$product->save();
But In this code,Customer id i increment by 1000 2001,3002 like this. so how should i avoid it ?
id | customer_id | name |
1 | 1000 | ABC |
2 | 2001 | Tripathi |
3 | 3002 | Leaptrig |
4 | 4003 | Falcon |
5 | 5004 | Savillan |
6 | 6005 | Molt |
7 | 7006 | Falt |
You can try this :-
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=$lastProduct+1;
}
$product->save();
I have read about this already in SO and MariaDB knowledgeable about this incompatibility between Mysql and Mariadb. But I am not sure how to resolve this issue in Laravel Eloquent / DB queries.
My Problem: The groupBy orderBy query gives different results in MariaDB and MySql. It works fine in mySql by the results are in different order in MariaDB.
This is my query:
$messages = ChatMessages::select(DB::raw('t.*'))
->from(DB::raw('(SELECT * FROM chat_messages ORDER BY created_at DESC) t'))
->whereIn('message_id', $messageIds)
->groupBy('message_id')
->orderBy('created_at', 'DESC')
->paginate(3);
For example, lets say this is the chat_messages table:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 1 | 1000 | 2017-01-01 06:03:40 | Anna |
+----+----------+---------------------+-----------+
| 2 | 1007 | 2017-01-02 07:13:20 | Becky |
+----+----------+---------------------+-----------+
| 3 | 1000 | 2017-01-03 08:20:12 | Christina |
+----+----------+---------------------+-----------+
| 4 | 1004 | 2017-01-03 08:20:15 | Dorothy |
+----+----------+---------------------+-----------+
| 5 | 1004 | 2017-01-04 09:25:45 | Emma |
+----+----------+---------------------+-----------+
| 6 | 1000 | 2017-01-05 10:30:10 | Fiona |
+----+----------+---------------------+-----------+
| 7 | 1007 | 2017-01-05 10:33:23 | Gigi |
+----+----------+---------------------+-----------+
| 8 | 1007 | 2017-01-06 12:46:34 | Heidi |
+----+----------+---------------------+-----------+
| 9 | 1000 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
| 10 | 1007 | 2017-01-07 14:58:37 | Jane |
+----+----------+---------------------+-----------+
| 11 | 1007 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
The query works fine in MySql database and the results are returned as this:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 11 | 1007 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
| 9 | 1000 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
| 5 | 1004 | 2017-01-04 09:25:45 | Emma |
+----+----------+---------------------+-----------+
However, in MariaDB database, the results are returned incorrectly like this. It seems to group the message_id in ascending order first and then adding the orderBy to that:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 4 | 1004 | 2017-01-03 08:20:15 | Dorothy |
+----+----------+---------------------+-----------+
| 2 | 1007 | 2017-01-02 07:13:20 | Becky |
+----+----------+---------------------+-----------+
| 1 | 1000 | 2017-01-01 06:03:40 | Anna |
+----+----------+---------------------+-----------+
I tried changing the query thought of using unique() instead like this:
ChatMessages::whereIn('message_id', $messageIds)
->orderBy('created_at', 'DESC')
->paginate(3)
->unique('message_id');
Although it works in MariaDB and MySql the same way, but the pagination is applied before the unique check and therefore returned lesser results:
+----+----------+---------------------+-----------+
| id | message_id | created_at | name |
+----+----------+---------------------+-----------+
| 11 | 1007 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
| 9 | 1000 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
How can I resolve this?
You are probabbly trying to do a "groupwise max". This can no longer be done by the trick of having a subquery with an ORDER BY.
A subquery, but definition, has no order. However, in the past, both MariaDB and MySQL would perform the ORDER BY, and that happened to be beneficial to the outer query.
MariaDB was first to ignore the inner ORDER BY; MySQL picked up on it later. Follow the tag [greatest-n-per-group] for various workarounds.
so I'm a BIRT beginner, and I just tried to get a real simple report from one of my tables of a postgres DB.
So I defined a flat table as datasource which looks like:
+----------------+--------+----------+-------+--------+
| date | store | product | value | color |
+----------------+--------+----------+-------+--------+
| 20160101000000 | store1 | productA | 5231 | red |
| 20160101000000 | store1 | productB | 3213 | green |
| 20160101000000 | store2 | productX | 4231 | red |
| 20160101000000 | store3 | productY | 3213 | green |
| 20160101000000 | store4 | productZ | 1223 | green |
| 20160101000000 | store4 | productK | 3113 | yellow |
| 20160101000000 | store4 | productE | 213 | green |
| .... | | | | |
| 20160109000000 | store1 | productA | 512 | green |
+----------------+--------+----------+-------+--------+
So I would like to add a table / crosstab to my birt report which creates a table (and after that a page break) for EVERY store which looks like:
**Store 1**
+----------------+----------+----------+----------+-----+
| | productA | productB | productC | ... |
+----------------+----------+----------+----------+-----+
| 20160101000000 | 3120 | 1231 | 6433 | ... |
| 20160102000000 | 6120 | 1341 | 2121 | ... |
| 20160103000000 | 1120 | 5331 | 1231 | ... |
+----------------+----------+----------+----------+-----+
--- PAGE BREAK ---
....
So what I tried in first was: Getting to work the standard CrossTab tutorial-template of BIRT.
I defined the DataSource, and created a datacube with dimension-group of 'store' and 'product' , and as SUM / detail -data the 'value' and for this example I just selected ONE day.
But the result looks like this:
+--------+----------+----------+----------+----------+-----+----------+
| | productA | productC | productD | productE | ... | productZ |
+--------+----------+----------+----------+----------+-----+----------+
| Store1 | 213 | | 3234 | 897 | ... | 6767 |
| Store2 | 513 | 2213 | 1233 | | ... | 845 |
| Store3 | 21 | | | 32 | ... | |
| Store4 | 123 | 222 | 142 | | ... | |
+--------+----------+----------+----------+----------+-----+----------+
It's because not every product is selled in every store, but the crosstab creates the columns by selecting ALL products available.
So, I just have no idea how to generate dynamicly different tables with different (but also dynamic) amount of columns.
The second step then would be to get the dates (days) to work.
But thanks in advance for every hint ot tutorial link to question one ;-)
You can just add a table with the complete datasource. Select the table and a group. Group by StoreID. You can set the pagebreak options for each grouping. Set the property for after to "always exluding last".
BIRT will add a group header. You can add multiple groupheader rows get the layout you're after.
For crosstabs it works in a similar way. After you added the crosstab to your page and set the info for the groups on rows and columns and added summaries. You can view the data. Select the crosstab and View the Row Area properties, select the pagegroup settings and add a new pagebreak. You can select on which group you want to break, choose your storeID group and select after: "always excluding last"
I'm trying find all the values in my hosts table, which do not contain partial match to values in my maildomains table.
hosts
+-------------------+-------+
| host | score |
+-------------------+-------+
| www.gmail.com | 489 |
| www.hotmail.com | 653 |
| www.google.com | 411 |
| w3.hotmail.ca | 223 |
| stackexchange.com | 950 |
+-------------------+-------+
maildomains
+---------------+
| email |
+---------------+
| gmail |
| hotmail |
| outlook |
| mail |
+---------------+
Specifically, I am looking to do SELECT * of hosts where the hosts.host NOT LIKE any value in '%.maildomains.email%'
Desired output:
+-------------------+-------+
| host | score |
+-------------------+-------+
| www.google.com | 411 |
| stackexchange.com | 950 |
+-------------------+-------+
Here's how I think it should work logically:
SELECT h.*, m.email FROM (SELECT h.* FROM hosts WHERE score > 100 as h)
h LEFT OUTER JOIN maildomains m ON (h.host LIKE CONCAT('%.',m.email,'%'))
WHERE m.email IS NULL
This results in error 10017: both left and right aliases encountered in join ''%''
I also managed to get a similar query to run without error as CROSS JOIN, but it yields bad results:
SELECT h.*, m.email FROM (SELECT h.* FROM hosts WHERE score > 100 as h)
h CROSS JOIN maildomains m
WHERE h.host NOT LIKE CONCAT('%.',m.email,'%')
+-------------------+---------+---------+
| p.host | p.score | m.email |
+-------------------+---------+---------+
| www.gmail.com | 489 | hotmail |
| www.gmail.com | 489 | outlook |
| www.gmail.com | 489 | mail |
| www.hotmail.com | 653 | gmail |
| www.hotmail.com | 653 | outlook |
| www.hotmail.com | 653 | mail |
| www.google.com | 411 | gmail |
| www.google.com | 411 | hotmail |
| www.google.com | 411 | outlook |
| www.google.com | 411 | mail |
| w3.hotmail.ca | 223 | gmail |
| w3.hotmail.ca | 223 | outlook |
| w3.hotmail.ca | 223 | mail |
| stackexchange.com | 950 | gmail |
| stackexchange.com | 950 | hotmail |
| stackexchange.com | 950 | outlook |
| stackexchange.com | 950 | mail |
+-------------------+---------+---------+
I appreciate any and all guidance.
You could do something like this:
select host from hosts h left outer join maildomains m on (regexp_replace(regexp_replace(regexp_replace(regexp_replace(h.host,'www.',''),'.com',''),'.ca',''),'w3.','') = m.email) where email is NULL;
If your Hive version is 0.13 or newer, than you could use a subquery in the WHERE clause to filter the rows from the hosts table. The following is a more generalized approach that would not require you to enumerate all of the top-level domains you might find in your data:
SELECT host, score
FROM hosts
WHERE
regexp_extract(hosts.host, "(?:.*?\\.)?([^.]+)\\.[^.]+", 1) NOT IN
(SELECT email FROM maildomains);
This approach isolates the portion of the host domain just before the TLD with the regexp_extract and then checks to see if that domain name occurs in the subquery on the maildomains table.