TYPO3 Extbase query based on other table - sql

I got to table where one related to the other
TABLE A
uid
name
TABLE B
uid
name
rel_table_a
Now I would like:
// find all table A that is related to by table B
public function findAllTableAThatIsRelatedToByTableB() {
$query = $this->createQuery();
// what should this line be?
//$query->matching($query->count(tableB.rel_table_a = uid));
return $query->execute();
}
Is that possible or will the relation need to be created in the TCA for Table A first.

You can use related tables in the queries, but first you need to set up your TCA and your models properly, otherwise extbase does not know how your tables are related.
As the query condition you could just check if the relation exists.
In your structure we can only do this query on the table B since your table A does not have a relation field, so in this example you will get all items from table B that have a table A element related.
$query->matching($query->greaterThan('rel_table_a.uid', 0));
If you have an 1:n relation from table A to table B, then you should use an inline field. This way you can do a query on the table A: https://docs.typo3.org/typo3cms/TCAReference/Reference/Columns/Inline/Index.html
PS: you can not use $query->count inside a $query->matching

Related

JOIN of DB table and internal table produces "is not defined in the ABAP Dictionary"

First: I'm working on a existing code and want to add some new stuff to it and I'm really new to ABAP
My goal: I want to duplicate an existing table and remove all values that occur multiple times. That - at least I think - works. Afterwards I want to INNER JOIN this new created table with another already existing table, but unfortunately I always get the following error:
Method MethodName "newCreatedTable" is not defined in the ABAP Dictionary as a table, projection view, or database view
Additional Info: As you can see I'm working inside of a method!
Here is my code what I've done so far:
creating new table and delete all duplicates
DATA newCreatedTable TYPE STANDARD TABLE OF existingTable.
SELECT columnName
FROM existingTable INTO TABLE newCreatedTable.
DELETE ADJACENT DUPLICATES
FROM newCreatedTable COMPARING columnName.
here is where the error happens
SELECT *
FROM anotherExistingTable as p
INNER JOIN newCreatedTable as t on t~columnName = p~columnName
...
I hope someone can help me out in this case! Thank You in advance!
If I'm not wrong your code looks like this:
DATA newCreatedTable TYPE STANDARD TABLE OF existingTable.
SELECT columnName FROM existingTable INTO TABLE newCreatedTable.
DELETE ADJACENT DUPLICATES
FROM newCreatedTable COMPARING columnName.
SELECT *
FROM anotherExistingTable
INNER JOIN newCreatedTable as t on t~columnName = p~columnName
If this is the case then you cannot make a SELECT on an internal table. You can only make this operation on a table that exists in the ABAP dictionary.
Your code should look like this:
DATA newCreatedTable TYPE STANDARD TABLE OF existingTable.
SELECT columnName
FROM existingTable INTO TABLE #newCreatedTable.
DELETE ADJACENT DUPLICATES FROM newCreatedTable COMPARING columnName.
SELECT *
FROM anotherExistingTable
FOR ALL ENTRIES IN #newCreatedTable " <-------------- Use FOR ALL ENTRIES
WHERE columnName = #newCreatedTable-columnName. " <-- in your code

SQL-Creating Table

I am stuck with this question. I know table can be created with the select statement.
CREATE TABLE new_orders (ordr_id,order_date DEFAULT SYSDATE,cus_id)
AS
Select order_id.order_date,customer_id FROM Orders;
Which statement would be true regarding the above question ?
The NEW_ORDERS table would not get created beacuse the default value cannot be specified in the column definition.
The NEW_ORDERS table would get created and only the NOT NULL constraint define on the specified columnns would be passed to the new table.
The NEW_ORDERS table would not get created because the column names in the CREATE TABLE command and the SELECT clause do not match.
The NEW_ORDERS table would get created and all the constraints defined on the specified columns in the orders table would be passed to new Table.
I think correct answer should be 3, but it's wrong. The correct answer according to my book is 2. I don't get it how?
The answer should not be 3. Because in the select statement, you are taking values from Orders table. So you will use the column names of that table. Selecting value from there, you are inserting it into new_orders. So, the column names don't have to be same because they are different tables.

Get parent name and schema of a inherited table

Suppose I have a PostgreSQL table called master.products and another called account.products. The second one inherits from the first.
Is is possible to create a query to get the parent name and schema of the table account.products?
You get this information from the system catalog pg_inherits.
SELECT inhparent::regclass::text
FROM pg_catalog.pg_inherits
WHERE inhrelid = 'account.product'::regclass;
The name is automatically schema-qualified to make it unambiguous according to the current search_path .
SQL Fiddle.
Related:
Check if table inherits from other table in PostgreSQL
About regclass:
How to check if a table exists in a given schema

Sql Column had no values

I have a sql table that I am trying to add a column from another table to. Only when I execute the alter table query it does not pull the values out of the table to match the column where I am trying to make the connection.
For example I have column A from table 1 and column A from table 2, they are supposed to coincide. ColumnATable1 being an identification number and ColumnATable2 being the description.
I tried this but got an error...
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200)
where v_venkey = v_vendorno
It tells me that I have incorrect syntax... Anyone know how to accomplish this?
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200);
go
update c
set c.V_VendorName = a.V_VendorName
from CommittedTbl c
join TableA a
on c.v_venkey = a.v_vendorno;
go
I'm just guessing at your structure here.
alter table 2 add column A <some_type>;
update table2 set column A = (select column_A from table2 where v_venkey = v_vendorno);
Your names for tables and columns are a bit confusing but I think that should do it.
There is no WHERE clause for an ALTER TABLE statement. You will need to add the column (your first two lines), and then insert rows based upon a relationship you define between the two tables.
ALTER TABLE syntax:
http://msdn.microsoft.com/en-us/library/ms190273%28v=sql.90%29.aspx
There are several languages within SQL:
DDL: Data Definition Language - this defines the schema (the structure of tables, columns, data types) - adding a column to a table affects the table definitions and all rows will have that new column (not just some rows according to a criteria)
DML: Data Manipulation Language - this affects data within a table, and inserting, updating or other changes fall into this and you can update some data according to criteria (and this is where a WHERE clause would come in)
ALTER is a DDL statement, while INSERT and UPDATE are DML statements.
The two cannot really be mixed as you are doing.
You should ALTER your table to add the column, then INSERT or UPDATE the column to include appropriate data.
Is it possible that you want a JOIN query instead? If you want to join two tables or parts of two tables you should use JOIN.
have a look at this for a start if you need to know more LINK
hope that helps!

Can't get SQL statement to update from 2 Tables?

I currently have a database with two tables in it one hold lets say job details and the other company details. The "company_name" exists in the "Jobs" table which will have a matching entry in the "Companies" table under the field of "name". I want to basically set the field in the "Companies" table of "comp_id" to be the value of the field "id" in the "Jobs" table, WHERE the "name" in the "Companies" table is equals to the "company_name" in the "Jobs" table.
I have create the query below which i believed should work however it returns no rows affected?? can anyone please help me with this?
UPDATE `jobs`, `companies`
SET `comp_id` = 'companies.id'
WHERE ('companies.name' = 'jobs.company_name')
Thanks
This condition:
WHERE ('companies.name' = 'jobs.company_name')
is one problem (unless this is a copy & paste error during posting)
You are comparing two string literals there (and of course as they are not the same, you will never update anything).
The reason is you are using single quotes which denote a string literal. To quote column names you either need to use double quotes or the backticks you had used before (assuming you are on MySQL).
Reading your question you are only updating one table not two?
I want to basically set the field in
the "Companies" table of "comp_id" to
be the value of the field "id" in the
"Jobs" table, WHERE the "name" in the
"Companies" table is equals to the
"company_name" in the "Jobs" table.
All you want to do is set the comp_id column in the companies table to the value of the id column in the jobs table where the name and company_name columns are the same?
To do this using MSSQL you would do something like this:
Update c
Set c.comp_id = j.Id
From dbo.Companies c
Join dbo.Jobs j on c.Name = j.Company_Name
You can update only 1 table in an UPDATE statement in SQL Server. To update multiple tables at once you have the following options:
Use Stored procs
Create a View of the select statement and delete from the view
Use triggers.