CakePHP subqueries to find using drop down box - cakephp

I have 2 tables Record and Genre and I need to filter the records table down by the Genre I select in the drop down.
The SQL I have is a subquery as follows:
SELECT * FROM records WHERE genre_id = (SELECT id FROM genres WHERE genre = 'Blues');
How can I add this into my RecrodsController using latest cakephp?

Try this
$this->Records->find('all')->where([
'genre_id IN' => $this->Genres->find('all')->select('id')->where([
'genre' => 'Blues'
])->extract('id')->toArray()
]);

Related

Getting items from one table that don't have a corresponding foreign key in another table

Hi I am working on a react app that is on a ruby on rails server setup, that uses postgres.
I have two tables in the database, movies and genres. In the movie table, there is a field genre id. And in the genre table its ID is how it links to movie table's genre id. I am trying to find all genres that don't have a corresponding id listed in the movie table. I have tried two different ways, with SQL directly.
sql = "SELECT count(*) FROM genres
LEFT JOIN movies
ON genres.id = movies.genre_id
WHERE genre.id = movies.genre"
genres_with_no_movie = ActiveRecord::Base.connection.exec_query(sql).rows[0][0]
The ruby on rails way.
genres_with_no_movie = Genre.joins(:movies).count()
Both of these give me the number of connections. It gives me places where a movie does have a corresponding genre. I am trying to find genres that don't have a movie that connects. I tried WHERE NOT EXISTS in place of where and that did not work.
Any help would be greatly appreciated.
LEFT JOIN allows you to retrieve the rows from the table genres and which have no correspondance in movies, ie movies.genre IS NULL :
SELECT count(*)
FROM genres
LEFT JOIN movies
ON movies.genre_id = genres.id
WHERE movies.genre IS NULL
An alternative perhaps quicker, and (imho more direct) uses NOT EXISTS instead of join.
select count(*)
from genres g
where not exists
(select null
from movies m
where m.genre_id = g.id
);

How to query to see the attributes in the views of Marketing list in CRM?

I would like to fetch the attribute names like Listname,type,member type as shown in fig:
I used the following query:
SELECT Name,
*
FROM SavedQuery
WHERE ReturnedTypeCode = 4300
AND StateCode = 0
AND Name LIKE 'Active Marketing Lists%'
You have to query from list entity, that will result the needed columns of Marketing List.
SELECT listname, purpose, listid, membertype, type, lastusedon
FROM list
ORDER BY listname DESC
SavedQuery is view definition stored in DB like Active Marketing Lists.
Update:
The view columns which are designed/defined in view will be stored in layoutxml & layoutjson columns of savedquery records.

How to return value of array of data from database in laravel 4.2?

I have two table content,category
1-Category table is two column name and cat_id
2-content has column title,name,cat_id,id
3-Suppose content has three multiple cat_id
4 now I have to fetch name from that value which is in category table
Please see Attached photo of content and category table
First pic is of content table
Second pic is of category table
public function AllStories()
{
$cats = DB::table('content')
->join('category', 'content.Genres', '=', 'category.Genres')
->get();
$AllStories=Content::all();
return View::make('AlLStories')->with('AllStories',$AllStories)->with('cats',$cats);
}
now I have to fetch name from that value which is in category table
Your table structure is wrong
If you want to store Category information for a user you need to have pivot table where you can have category id against each content id.
Currently your content table have category as string since it's comma separated where as your category table have id as integer that's the reason you can not join them.
Table Structure
Author: id, author_name, ...
Category: id, category_title, Genre, ....
Content: id, title, ....
Content_Category: content_id, category_id
Author_Content: author_id, content_id
other version of pivot table could be (depending on your requirement)
Content_Category_Author: author_id, content_id, category_id
Now if you have relation like these you can do all kind of joins to fetch any require data.

Update matching column with the selected top 1 column

I have two tables Role and Role_Imp. I need to fetch Name column's value from the 1st row of the Role table. After that I need to update Name's column value in Role_Imp table for the rows which has Names same as the selected name from the Role table.
I am using the following query, which is not working as its wrong.
UPDATE Role_Imp
SET Role_Imp.Name = 'Role Test Change'
FROM Role_Imp
INNER JOIN
Role ON Role_Imp.Name = SELECT TOP 1 Name FROM Role
How should I do this?
Seems like this should do it:
UPDATE Role_Imp
SET Name = 'Role Test Change'
WHERE Name = (SELECT TOP 1 Name FROM Role)

How to customise ordering of results (i.e. something other than alphabetical order for strings)

I have a table "Category" in sql server2008. It has 2 columns -ID,Name. I have inserted 3 Name in it as:
1.Case Report
2.Original Article
3.Letter to Author
4.Submmited Article
I have used following query to show table data:
select * from Category order by Name desc
it is showing result as:
4.Submmited Article
2.Original Article
3.Letter to Author
1.Case Report
But I want to show table value as:
2.Original Article
1.Case Report
3.Letter to Author
4.Submmited Article
please help me someone.
SELECT *
FROM Category
ORDER BY CASE
WHEN NAME = 'Original Article' THEN 1
WHEN NAME = 'Case Report' THEN 2
WHEN NAME = 'Letter to Author' THEN 3
END ASC
Given that you do not want to use an alphabetic sorting, I would suggest adding another column to perform the ordering by. For example, create a table like this:
CREATE TABLE MY_TABLE (
ID int PRIMARY KEY,
NAME varchar(50),
SORT_ORDER int
)
You would populate the SORT_ORDER column data to match the ordering you need and can then get the sorted data with:
SELECT * FROM MY_TABLE ORDER BY SORT_ORDER
Hope that helps.
It looks like you are trying to do a custom sort, in that case you can add third column eg SortID int, and then order the result set by the SortID. eg
SELECT ID, Name
FROM TableName
ORDER BY SortID

Resources