Cannot do Inner join in CDbCriteria - join

This is mysqli query
SELECT DISTINCT t.company_id,t.image,t.text,t.date, t.title AS c_title
FROM news t
INNER JOIN companies c ON c.company_id=t.company_id ORDER BY t.date DESC
LIMIT 20" or die ("ERROR ". mysqli_error($link));
I want to write in CDbCriteria
$Criteria = new CDbCriteria();
$Criteria->join = 'INNER JOIN companies c ON t.company_id=c.company_id';
if ($place>0){
$Criteria->condition = "t.company_id = :place";
$Criteria->params = array(':place'=>$place);
}
$Criteria->order = "t.date DESC";
$Criteria->limit = 20;
$Criteria->select='t.company_id,t.image,t.text,t.date,c.title AS c_title';
$dataProvider = new CActiveDataProvider('News',
array(
'criteria'=>$Criteria,
'pagination'=>false
)
);
Error Property "News.c_title" is not defined

The property c_title is not defined in your News model (fields from your news table will be available as properties automatically, but c_title is an alias of title and not a field of of your table).
Put this in your News model:
public $c_title;

Related

select all records from left table that do not exist in right table

I am working on sql server I have two tables and I need to return records from the left table which are not found in the right table for that I am using left join like below query,
select #MID=MID,#MName=Name,#PID=PID,#PName=PName,#DID=DID from #CompanyDataInfo where id=#MCount
insert into #temp SELECT Top(1) f.Name,f.PID,f.PName,v.* FROM #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where v.Id =#MID and v.DId = #DId and v.PId = #PId and v.CId =#CId and DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() order by DATE_TIME desc
Result should be all rows from #CompanyDataInfo table while no record found in Employee table for related ID, I googled and use "v.Id is null" but not getting expected result
Is there any solution greatly appriciable
Thanks In advance
Your query is not using left join in correct way. You are using your right table reference in where clause. I try to correct it below but I don't have full information about your table schema. Please try this-
select
#MID = MID,
#MName = Name,
#PID = PID,
#PName = PName,
#DID = DID
from #CompanyDataInfo
where id = #MCount
insert into #temp
select
f.Name,
f.PID,
f.PName,
v.*
from #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where f.Id = #MID and
f.DId = #DId and
f.PId = #PId and
f.CId = #CId and
f.DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() and
v.Id is null
order by f.DATE_TIME desc
Add ...and v.Id is null to your where clause.

How to use WHERE for foreign key of a JOINed table in Doctrine?

I have a structure of three tables/entities: User (table users; columns id and name), Room (table roome; columns id and number), and RoomUser (user_room; columns id, user_id, room_id).
Now I want to retrieve all Rooms for a User with a given id. How to do this without to join Rooms?
$userId = 123;
// ...
$queryBuilder = $this->entityManager->createQueryBuilder();
$query = $queryBuilder->select('r')
->from(Room::class, 'r')
->join('r.RoomUsers', 'ru')
->where('ru.room_id = :userId') // room_id? ru.Room.id?
->setParameter('userId', $userId)
->getQuery();
$rooms = $query->getResult(Query::HYDRATE_OBJECT);
So in SQL it would be something like
SELECT *
FROM rooms
JOIN room_users ON room_users.room_id = rooms.id
WHERE user_id = 123;
How to implement this simple request with the QueryBuilder?
You can use MEMBER OF (docs) for that:
$query = $queryBuilder->select('r')
->from(Room::class, 'r')
->where(':user_id MEMBER OF r.users')
->setParameter('user_id', $userId)

jql information in database

Here is a jql query that I got the result of
assignee in membersOf("project")
This would return the issues of members belonging to project.
I would like to know in which table of jira database would this data(or this link of which member belong to which proj) is stored?
Group Membership
The group memberships are stored in the CWD_MEMBERSHIP table.
Example:
SELECT LOWER_CHILD_NAME
FROM CWD_MEMBERSHIP
WHERE MEMBERSHIP_TYPE = 'GROUP_USER'
AND LOWER_PARENT_NAME = 'jira-administrators';
Example2, to fetch the user infos as well:
SELECT
U.*
FROM
CWD_MEMBERSHIP M
INNER JOIN CWD_USER U
ON
M.LOWER_CHILD_NAME = U.LOWER_USER_NAME
WHERE
M.MEMBERSHIP_TYPE = 'GROUP_USER' AND
M.LOWER_PARENT_NAME = 'jira-administrators';
Project Role Membership
The project role memberships however are in the PROJECTROLE and PROJECTROLEACTOR tables.
Example:
SELECT A.ROLETYPEPARAMETER AS USERNAME, R.NAME AS ROLENAME, P.PKEY || ' - ' || P.PNAME AS PROJECTNAME
FROM PROJECTROLEACTOR A
INNER JOIN PROJECTROLE R ON A.PROJECTROLEID = R.ID
INNER JOIN PROJECT P ON A.PID = P.ID
WHERE P.PKEY = 'YOUR_PKEY_COMES_HERE'
ORDER BY 3, 1, 2;
Example2, to get users that are explicitly assigned to project roles (not through groups):
SELECT A.ROLETYPEPARAMETER AS USERNAME, R.NAME AS ROLENAME, P.PKEY || ' - ' || P.PNAME AS PROJECTNAME
FROM PROJECTROLEACTOR A
INNER JOIN PROJECTROLE R ON A.PROJECTROLEID = R.ID
INNER JOIN PROJECT P ON A.PID = P.ID
INNER JOIN CWD_USER U ON LOWER(A.ROLETYPEPARAMETER) = U.LOWER_USER_NAME
ORDER BY 3, 1, 2;
Issue Change History
To get the issue history, you'll need the changegroup and changeitem tables joined to jiraissue. Changegroup stores who changed and when, changeitem contains the olda and new data, alongside what field was changed.
Example of listing ex-assignees:
SELECT
CG.AUTHOR AS CHANGE_USER ,
CG.CREATED AS CHANGE_WHEN ,
CI.FIELD AS CHANGED_WHAT,
CI.OLDVALUE AS CHANGED_FROM,
CI.NEWVALUE AS CHANGED_TO
FROM
JIRAISSUE JI
INNER JOIN CHANGEGROUP CG
ON
JI.ID = CG.ISSUEID
INNER JOIN CHANGEITEM CI
ON
CG.ID = CI.GROUPID
WHERE
JI.PROJECT = 10100 AND
JI.ISSUENUM = 1234 AND
CI.FIELDTYPE = 'jira' AND
CI.FIELD = 'assignee'
ORDER BY
CG.CREATED ASC;
The last row's (newest created) newvalue must match jiraissue.assignee-s value.

How to add woocommerce classname to top sell product

How can I add a classname of "topseller" to the top sell products on woocommerce?
Is there a function that checks whether product is top sell?
I did. Here is the solution;
first I generated a query which gets 10 products that have biggest total_sales
$query = "SELECT ID FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON ( pm.post_id = p.ID AND pm.meta_key='total_sales' )
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
ORDER BY pm.meta_value+0 DESC
LIMIT 10";
$topselled_ids = $wpdb->get_results( $wpdb->prepare($query,OBJECT ));
//print_r ($topselled_ids );
foreach ($topselled_ids as $one_topselled_id)
$topselled[] = $one_topselled_id->ID;
Then I checked if id of current product is in array (in the content-product.php) .
if ( in_array($post->ID, $topselled) )
$classes[] = "top_selled";

EF 4.0 Linq to Enity query

Hi guys i need to replicate this SQL query in Linq to Entity
select * from Subscriber a
inner join User b on a.UserId = b.Id
where b.Username = 'Name'
May be some one may help.
Try this:
var query = from s in context.Subscribers.Include("User")
where s.User.Username == "Name"
select s;
This suppose that Subscriber has navigation property User referencing the user instance.
If you wan to use join (which is not needed) you can use this for inner join:
var query = from s in context.Subscribers
join u in context.Users on s.User.Id equals u.Id
where u.Username == "Name"
select new
{
Subscriber = s,
User = u
};
or this for left outer join:
var query = from s in context.Subscribers
join u in context.Users on s.User.Id equals u.Id into x
where u.Username == "Name"
from y in x.DefaultIfEmpty()
select new
{
Subscriber = s
User = y,
};

Resources