I'm using the following query in snowflake:
SELECT nested_flattened_items.name
FROM HEVO.SHIPSTATION.SHIPSTATION_ORDERS,
LATERAL FLATTEN(input => HEVO.SHIPSTATION.SHIPSTATION_ORDERS.ITEMS) flattened_items,
LATERAL FLATTEN(input => flattened_items.value) nested_flattened_items
However, when I run it, it says: Error: invalid identifier 'NESTED_FLATTENED_ITEMS.NAME' (line 36)
Whenever I run a query to check if the key exists, it runs and I blatantly notice the key "name."
SELECT nested_flattened_items.*
FROM HEVO.SHIPSTATION.SHIPSTATION_ORDERS,
LATERAL FLATTEN(input => HEVO.SHIPSTATION.SHIPSTATION_ORDERS.ITEMS) flattened_items,
LATERAL FLATTEN(input => flattened_items.value) nested_flattened_items
Any idea how to resolve?
enter image description here
Related
I am using WordPress platform, and using WP_Query to call my data although can use custom MYSQL script if if can work.
I just want to add a where condition which can filter JSON value as below explained:
so in postmeta table i have a following columns
meta_key: _stock_reserve_outlet_extended
meta_value: a:2:{s:8:"outlet_2";a:2:{s:5:"stock";s:2:"20";s:5:"rider";s:2:"-1";}s:8:"outlet_1";a:2:{s:5:"stock";i:-4;s:5:"rider";i:0;}}
which is actually an array extracted as below by get_post_meta() function:
Array (
[outlet_2] => Array (
[stock] => 45
[rider] => 0
)
[outlet_1] => Array (
[stock] => -4
[rider] => 0)
)
)
i want to filter rows during calling rows from script by comparing following:
stock > 0 in outlet_2
I tried to search filter json values filtration by json_search but that does not compare by key.
To give WP_Query a try, i use following but nothing effect shows
$args['meta_query'][] = array(
'key' => '_stock_outlet_extended',
'value' => array(
'key' => 'stock',
'value' => '18'
),
'compare' => '>',
);
Then i tried following script but that not work aswell:
SELECT meta_value
FROM `frbl1ozme_postmeta`
WHERE `post_id` = 3699 AND `meta_key` = '_stock_outlet_extended' AND meta_value ->'$.stock' > 1;
Do anyone know how to tackle this sort of criteria?
what i observer is if you are saving any array by using update_post_meta() function of wordpress then it will serialize it and you are unable to filter rows by script then.
So after understood this fact i made the changes in structure and along with the stock i am saving status according to the value condition of stock, so i can filter rows easily by MYSQL.
So in one line answer, one can not achieve filter serialize rows via mysql queries till date.
Thank you all for the Help, specially #RiggsFolly
I have two tables. The first - Product, the second - Category. They contain fields with the same name - 'name'.
In model Product I added following code:
public function getCategory(){
return $this->hasOne(Category::className(), ['id' => 'cat_id']);
}
I need to show in GridView the column from table Category. I added following code for this in the model ProductSearch:
$query->joinWith(['category' => function($query) { $query->from(['cat' => 'category']); }]);
This code adds the alias cat for the table Category.
After that I got an error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `product` LEFT JOIN `category` `cat` ON `product`.`cat_id` = `cat`.`id` WHERE `name` LIKE '%aasdadadsdgdgdg%'
Error Info: Array
(
[0] => 23000
[1] => 1052
[2] => Column 'name' in where clause is ambiguous
)
How can I add the alias for the table Product?
Open your ProductSearch model, navigate to the search($params) method. Below you should see the filtration part:
$query->andFilterWhere(['like', 'name', $this->name])
fix the ambigous part by writing table name product to that line.
$query->andFilterWhere(['like', 'product.name', $this->name])
or..
$query->andFilterWhere(['like', self::tableName() . '.name', $this->name])
This'll giving precise info that name should be queried from product.name table.
The interesting part is that you don't get the error until you join the Category table.
Imho, it's the worst to find this errors as it seems like everything is working, until search functionality is used.
Let's say I have 10 books in a database. When I sort by the Name, I want the first book to have a 'rank' of 1. And so on and so forth, within the query.
I cannot use the auto incremented id.
[1] A
[2] B
[3] C
Etc.
My scenario is coins and using Yii2.
$coins = Coins::find()
->select(['id', 'name', 'icon', 'volume_24_hours', 'market_cap', 'price', 'change_24_hours', 'circulating_supply', 'abbreviation'])
->where(['is_fiat' => false])
->orWhere(['is_fiat' => null])
->orderBy(['volume_24_hours' => 'DESC']);
In Oracle, Postgre and MSSQL you can use some form of row_number function. In MySql you can emulate it with the help of variables.
Hope this works for you.. (:
SELECT ROW_NUMBER()OVER(ORDER BY BookName ASC) AS RowNo,
BookName
FROM TableName
How could I create a sub-query in cakePHP with find method? For example:
SELECT *, (SELECT COUNT(*) FROM table2 WHERE table2.field1 = table1.id) AS count
FROM table1
WHERE table1.field1 = 'value'
!!! table2.field1 = table1.id !!!
Just for addition, you can build subquery using Cake's ORM. It will be more easy. Please read CakePHP retrieving data doc
In general you can use buildStatement() method of DataSource object. You can save all logic, including pagination etc.
You can do this one of two ways:
1. Use $this->Model->query(...)
This allows you execute SQL directly using the query you posted above but be aware that it can make your application quite brittle if the database schema were to change. Probably the fastest.(Documentation)
Example
$this->Model1->query("SELECT * FROM model;");
2. Separate the Calls
This is probably not as fast as option 1 but it does give you the ability to break your query down into a number of steps. You're also unlikely to get SQL injection which is potential risk with option 1.
Example
$model1s = $this->Model1->find
(
'all',
array
(
'conditions' => array('Model1.field1' => 'value')
)
);
foreach($model1s as $model1)
{
$model1['model2_count'] = $this->Model2->find
(
'count',
array
(
'conditions' => array
(
'Model2.field1' => $model1['id']
)
)
);
}
I have a MySQL table with 3 columns (thread_id, message_id, message). Along the lines of the solution found under the "Example using GROUP BY" in this link, I want my query to GROUP BY thread_id, but return the line of of the highest message_id (instead of default lowest) for each thread_id. I then want a nicely formatted array with lines/items just like you get for less complex find operations in CakePHP along the lines of $array[index]['Model']['field']. Using the following CakePHP syntax:
$this->Model->find('all', array(
'fields' => array('MAX(Model.message_id) as message_id', 'Model.thread_id', 'Model.message'),
'group => 'Model.thread_id'
));
Now, unfortunately I am not getting that nicely formatted array. Instead I get an array which looks something like:
Array ( [0] => Array ( [0] => Array ( [message_id] => wanted/correct_message_id ) [Model] => Array ( [message] => Message from lowest/unwanted message_id line. [thread_id] => Key from lowest/unwanted message_id line))
Why does the message_id not get hooked onto the [Model] part of the array and why does CakePHP fetch the lowest message_id line and put the message and thread_id into the [Model] part of the array without the message_id column?
I want all thre columns in the [Model] part of the array and I want that line to be the highest message_id for that thread_id per my initial description. Hope this question makes sense.
Virtual fields are really useful for this kind of thing.
class MyModel extends AppModel {
public $virtualFields = array(
'max_message_id' => 'MAX(MyModel.message_id)'
);
}
You can now use max_message_id as if it were a normal field in your table, so you can add it to your find operations.