In a table I have news posts with these fields:
Title
Content
OwnerID
And a users table
ID
Name
Surname
The OwnerID relates to the ID in the users table, how can I get the name of the user who's ID matches the OwnerID?
I'm writing a website in ASP.net (VB).
You would need to join the two tables together like this:
select users.Name
from news inner join users
on OwnerID = ID;
This query has no where clause to filter the results that are returned so this query would return all users who are associated with a news record. If you wanted to find users associated with a specific news record you would need to filter on the news title or content like this:
select users.Name
from news inner join users
on OwnerID = ID
where Title = 'Some Title';
Related
I need to add the field of ProductName filterable in the ProductLookup field in retailPeriodicDiscount form. It is strongly connected to EcoResProduct and I can't use any other lookup field or method.
So, I need to add the product name field to productLookup method. Any suggestions how to do it?
For the product name you have to add the field Name of the table EcoResProductTranslation .Add the table as a joined datasource to the query and filter it by the system language.
sysTableLookup.addLookupfield(fieldNum(EcoResProduct, DisplayProductNumber));
sysTableLookup.addLookupfield(fieldNum(EcoResProduct, SearchName));
sysTableLookup.addLookupfield(fieldNum(EcoResProduct, ProductType));
sysTableLookup.addLookupfield(fieldNum(EcoResProductTranslation, Name));
if (_groupMember.Category)
{
query = RetailGroupMemberLineQueryProvider::containedProductsQuery(_groupMember.Category, true /*includesubcategories*/, _dataAreaId);
}
else
{
query = RetailGroupMemberLineQueryProvider::containedProductsQuery(_groupMember.Category, true /*includesubcategories*/, _dataAreaId);
}
// add datasources and join
qbdsProduct = query.dataSourceTable(tableNum(EcoResProduct));
qbdsProductTranslation = qbdsProduct.addDatasource(tableNum(EcoResProductTranslation));
qbdsProduct.relations(true);
// range for system language
qbdsTranslation.addRange(fieldNum(EcoResProductTranslation, LanguageId)).value(SystemParameters::getSystemLanguageId());
sysTableLookup.parmQuery(query);
I want to retrieve the customer name. But in table CustTable there is no field for the customer name. From which table can the customer name be retrieved?
You can retrieve the name using the method name() in table CustTable.
Select the table CustTable and then pass on the result of the method name().
Example:
CustTable custTable;
select custTable where custTable.AccountNum == "YourCustomer";
info(custTable.name());
Although I know this is trivial I'm stuck trying to implement the pagerfanta Paginator using the DoctrineORMAdapter, I want to paginate all entities sorted by id in descending order, the final SQL I want is this:
SELECT id, name FROM User ORDER BY id DESC LIMIT 0, 5;
Suppose I had users from A to Z and I want to limit them by 5 each page, what DoctrineORMAdapter is paginating results in User E to A listed in the first page, while what I actually expect is to see User Z to user V in the first page, U to Q in the second page and so on. The DQL I'm passing to DoctrineORMAdapter is as follow:
SELECT u FROM My\FluffyBundle\Entity\User u ORDER BY u.id DESC
On execution this is the final DQL for the first page:
SELECT DISTINCT id0 FROM (SELECT u0_.id AS id0, u0_.name AS name1 FROM User u0_
ORDER BY u0_.id DESC) dctrn_result LIMIT 5 OFFSET 0
Please note that when using the ArrayAdapter instead of DoctrineORM's it works as expected, but it's not a good idea to rely on ArrayAdapter when you have thousands of complex Doctrine Entities, not even with extra lazy loading :D.
This is the only relevant code:
$queryBuilder = $repo->createQueryBuilder('u')->orderBy('u.id', 'DESC');
$adapter = new DoctrineORMAdapter($queryBuilder);
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(5);
Thanks.
This will help you:
$adapter = new DoctrineORMAdapter($queryBuilder, false);
Had the same problem this morning.
By default Pagerfanta is treating your query as one with joins. Setting second argument to false makes it use simple query handling.
In Kunstmaan Bundle, in AdminListConfiguration class, you have to overide function that is creating Pagerfanta, if you want to sort simple entity.
I was trying to do a 'Latest Products' page for an eShop project I'm working on. Basically, I want to show a number of products, say 10, from a table in my db that where added in my database in the last 30 days or less.
First I tried to use the GridView function in VB.NET, where it auto-populates the table but can't be limited and then I tried this SQL statement, which isn't working, giving me an error.
SELECT *
FROM Product
WHERE DateAdded > (SELECT DATEADD(d,-30,(SELCT MAX(DateAdded) FROM Product)) AS "Last 30 Products Added");
ORDER BY DateAdded DESC
My database has a table called 'Product' which has various columns:
ProductID
CategoryID
ProductModelNo
ProductImage
ProductName
UnitName
ProductActive
DateAdded
Any idea on how I can solve the problem ?
You are looking for the DATEDIFF function to compare the DateAdded with NOW().
How do i put a nested insert into database using Linq to SQL
my Table is like this:
[Post]
--Post ID
--Post Title
[Attachment]
--Post ID (PFK)
--AttachmentID (PK)
--IMGID (FK)
--VIDID (FK)
[IMG]
--IMGID (PK)
it's a M:N table
basically when i submit a form , it go together with the attachments within the form. Im ok with insert Post, but strugling with the inserting attachments since i dont have any POST ID yet...
so what do you think is the best solution for this kind of problem?
Thank you
If you create a two-way Association between Post and Attachment, so you can do the following:
Post post = new Post { /* Set some fields perhaps */ };
Attachment attachment = new Attachment { /* Same here */ };
attachment.Post = post; // That is the association you need to create
dataContext.Posts.InsertOnSubmit(post);
dataContext.SubmitChanges();
LINQ-to-SQL will then handle the insertion of the Attachment(s) automatically.
I haven't used the DBML designer in a while, but I'm fairly sure you can say that a Post has Attachments and that an Attachment has a Post. And when you say attachment.Post = post, the setter for Post internally will do _post.Attachments.Add(attachment) where _post is a private member of the Attachment entity. Then, when you submit the Post, it will loop through the Attachments and insert them too.