Yii2 - Table alias with ActiveRecord - yii

I have a table with a long name, example products_with_a_long_name. The Model name is ProductsWithALongName.
Now, I have a query where I need to select many columns from this table while joining with another table. Example:
ProductsWithALongName::find()
->select(['products_with_a_long_name.id', 'products_with_a_long_name.selling_price','client.name'])
->leftjoin('all_the_clients as client','products_with_a_long_name.clientId = client.id')
->where(['products_with_a_long_name.id' => $var]);
Now how can I use an alias for the first table, products_with_a_long_name as I'm using an alias for the second. I know I can use Query but in this case I need the result to be ActiveRecord so this is not an option in this case.

You can use alias():
ProductsWithALongName::find()
->alias('p')
->select(['p.id', 'p.selling_price','client.name'])
->leftjoin('all_the_clients as client', 'p.clientId = client.id')
->where(['p.id' => $var]);

Related

SQL Parameter to Include All on ID Column

I'm just taking a look at the following query
select * from tablename
where id like '%%';
So that it can handle parameters to include all of the data or filtered data like bellow
select * from tablename
where id like '%1%';
Which is fine for most parameters I use but this seems wrong for an ID because it will return all data that has IDs containing 1 which I don't want
To get around this I can only append the where clause if the ID is given but that seems like a pain in the butt
Is it possible to use a different type of where clause so that a wildcard can be used in a where equals clause instead of a where like clause, example
select * from tablename
where id = '*';
So that the same query can be used to return all or filtered data? Pass parameter '*' for all or parameter '1' for ID 1 specifically
(I'm not sure if it matters for this case but I'm using PostgreSQL 9.6.12 in this example)
This would often be expressed as:
where (id = :id or :id is null)
null is the "magic" value that represents all rows.

Sequelize subquery in from clause

I am using POSTGRESQL with sequelize.js and would really like to execute a query which looks like this
SELECT "CT_Foo"."cola", "CT_Foo"."colb", "CT_Foo"."colc",
"CT_Foo"."cold", "CT_Foo"."cole", "CT_Foo"."colf",
COUNT(*)
FROM (
SELECT "CT_BAR"."cola", "CT_BAR"."colb", "CT_BAR"."colc",
"CT_BAR"."cold", "CT_BAR"."cole", "CT_BAR"."colf",
"CT_BAR"."colg"
FROM public."Customers" AS "CT_BAR"
WHERE ("CT_BAR"."colf" IN ('SOMEID') AND "CT_BAR"."colg" ?
'date')
) AS "CT_Foo"
WHERE ("CT_Foo"."colf" IN ('SOMEID'))
GROUP BY "CT_Foo"."cola", "CT_Foo"."colb", "CT_Foo"."colc",
"CT_Foo"."cold", "CT_Foo"."cole", "CT_Foo"."colf"
Columns A-F are text and G is JSONB
Basically the reason why I am doing this is because I need to group on columns A-F with a query on column F and if something exists in the JSONB in column G, I do not wish to include column G in the grouping, because it's JSON and I'm checking for the existence of a date... This seems a simple way to do this. I know postgresql has CTE's but sequelize does not support them. I believe another name for this is "Derived Tables"
I can form a query normally, but cannot get the subquery into the FROM clause.
Any idea on how todo this or get the same result?

select from two sql views not using one of them

I have this sql view, this is not my code:
SELECT
`view_combined_rev_data`.`date` AS `date`,
`view_combined_rev_data`.`book_title` AS `book_title`,
`view_combined_rev_data`.`marketplace` AS `marketplace`,
`view_combined_rev_data`.`amazon_kdp_avg_list_price` AS `amazon_kdp_avg_list_price`,
`view_combined_rev_data`.`amazon_kdp_royalty_type` AS `amazon_kdp_royalty_type`,
`view_combined_rev_data`.`amazon_kdp_revenue_in_usd` AS `amazon_kdp_revenue_in_usd`,
`view_combined_rev_data`.`amazon_kdp_royalty_in_usd` AS `amazon_kdp_royalty_in_usd`,
`view_combined_rev_data`.`amazon_kdp_paid_downloads` AS `amazon_kdp_paid_downloads`,
`view_combined_rev_data`.`amazon_kdp_free_downloads` AS `amazon_kdp_free_downloads`,
`view_combined_rev_data`.`amazon_ku_pages_read` AS `amazon_ku_pages_read`,
`view_combined_rev_data`.`amazon_ku_revenue_in_usd` AS `amazon_ku_revenue_in_usd`,
`view_combined_rev_data`.`create_space_revenue_in_usd` AS `create_space_revenue_in_usd`,
`view_combined_rev_data`.`create_space_royalty_in_usd` AS `create_space_royalty_in_usd`,
`view_combined_rev_data`.`create_space_paperbacks_sold` AS `create_space_paperbacks_sold`,
(
(
`view_combined_rev_data`.`amazon_kdp_revenue_in_usd` + `view_combined_rev_data`.`amazon_ku_revenue_in_usd`
) + `view_combined_rev_data`.`create_space_revenue_in_usd`
) AS `daily_total_revenue`,
(
(
`view_combined_rev_data`.`amazon_kdp_royalty_in_usd` + `view_combined_rev_data`.`create_space_royalty_in_usd`
) + `view_combined_rev_data`.`amazon_ku_revenue_in_usd`
) AS `daily_total_royalty`
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
My question is simple:
Why view_combined_rev_marketplace_data is used in this line. I can't find the code using it anywhere else, so can I simply remove it?
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
This is your FROM clause:
FROM `view_combined_rev_marketplace_data` `view_combined_rev_data`
The first name, view_combined_rev_marketplace_data is the name of a table or view (presumably a view) that exists in the database.
The second name, view_combined_rev_data, is a table alias. This is how the table/view is referred to in the query.
I recommend that you use table/view aliases that are abbreviations for the table/view name, something like this:
FROM `view_combined_rev_marketplace_data` vcrmd
Then the references to columns would look like:
SELECT vcrmd.`date` AS `date`,
vcrmd.`book_title` AS `book_title`,
. . .
And this would further simplify to:
SELECT vcrmd.`date`,
vcrmd.`book_title`,
. . .
The column alias (name given after the as) is unnecessary in this case, because it defaults to the column name. Note, though, that local coding styles may recommend having explicit column aliases.
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
These are not two views, but one view view_combined_rev_marketplace_data with an alias view_combined_rev_data.
When an alias is used to reference a table/view/function, then it must be used in the statement instead of the object's name. Usually aliases are meant to provide a shorter or more readable reference to the SQL object. In this case it is relatively long.

SQL query with regex

I have a table vehicles has a column 'name'
Values are stored like car/tesla, car/honda, truck/daimler (each value is stored as type/brand)
I want to query the table using only brand. If I look up tesla, it should return the row corresponding to car/tesla. How do I do it? I'm using postgres.
There is no need in regex in your case. Just use old good like:
select name
from vehicles
where name like '%/tesla'
2 solutions are available a select query with LIKE operand or a select query with contains operand.
select * from vehicles where name LIKE '%tesla%'
Select * from vehicles where Contains(name, "tesla");

Laravel Query builder: left join, select, and where not working

Unfortunately the Laravel query builder seems not in a good mood!
Here is my table structure:
Table user structure
ID, USERNAME, NAME, USER_ROLE_MODULE_ID
Table user_role_module structure
ID, NAME
And this is my query builder statement
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);
Goal:
I just want to get all data from user + user role name from user_role_module AND also applies in WHERE statement so I can use it for search feature.
I am getting result from WHERE statement without specifying table name. because it already stated which table and column to select from SELECT statement.
The problem:
if I search for name, it return error ambiguous column //Laravel is confusing whether taking name from user table or user_role_module table!
if I search for user_role, then the column doesn't exist
Why is it? What is the solution?
The problem:
user.NAME <-- both these columns are called NAME
user_role_module.NAME <--
You have multiple problems with your Laravel query. As #Viktor correctly pointed out, your join condition is wrong. You should be joining user.USER_ROLE_MODULE_ID to user_role_module.id. But the immediate cause of your error I believe is that both tables have a NAME column.
Just specify the columns you want using both table and column name. This way, no column name would be ambiguous.
DB::table('user')
->leftJoin('user_role_module', 'user.USER_ROLE_MODULE_ID', '=', 'user_role_module.id')
->select('user.ID', 'user.USERNAME', 'user.NAME AS userName',
'user_role_module.name as user_role')
->where("name LIKE '%John%'")
->paginate(10);
Change this:
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);
with this:
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->where('user.name', 'like', '%Jhon%')
->select('user.*', 'user_role_module.name as user_role')
->get();
and make a custom pagination because select is not working with paginate.