Get Wordpress posts with images link - android

I want to get wordpress posts with specific category and link of images.
As you know images links save to database in guid column, when post_type = attachment.
and ID of post and post_parent are the same.
Now I want to get posts and join guid column to same ID.
When I added Inner join to combine attachment and post, I got error!
Please help me, if you know the way that I can get post with specific category and images link of each post.
Here is my code:
SELECT
*
FROM
wp_posts p,
wp_postmeta m,
wp_terms t,
wp_term_taxonomy tt,
wp_term_relationships tr,
wp_terms t2,
wp_term_taxonomy tt2,
wp_term_relationships tr2
LEFT JOIN wp_posts AS p2
ON
p.ID = p2.post_parent
WHERE
p.post_type = 'post' AND p.post_status = 'publish'
AND p.ID = tr.object_id
AND t.term_id = tt.term_id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'category'
AND tt.term_id = t.term_id
AND t.name = 'Fashion'
GROUP BY
p.ID
ORDER BY
id
DESC
MySQL said:
#1054 - Unknown column 'p.ID' in 'on clause'

I suspect that the problem is due to mixing the old school comma syntax with the newer JOIN keyword.
Relevant excerpt from MySQL Reference Manual:
INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).
However, the precedence of the comma operator is less than that of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.
The easiest way to avoid this problem is to ditch the old school syntax for the join operation, use the JOIN keyword instead.
(It's great that the comma syntax is still valid, to provide backwards compatibility with existing SQL. But there's no good reason new development should use the comma syntax.)
Aside from that, there's a couple of big rock issues that stick out to me.
Seems like there's a lot of join conditions missing
Using * for the SELECT list in development can be useful shortcut, but we usually list the expressions we need to return, especially if we want to return id column from multiple tables, where we like to assign a column alias to avid duplicate columns names.
Relying on the non-standard extension to GROUP BY (when only_full_group_by is omitted from sql_mode to return values from "some" row in the collapsed group
Those all look like serious problems to me.
We can re-write the OP query to use JOIN keyword in place of comma syntax, and relocating conditions to the ON clause, this highlights what looks like missing join conditions:
SELECT *
FROM wp_posts p
JOIN wp_postmeta m
-- ON ???
JOIN wp_terms t
ON t.name = 'Fashion'
JOIN wp_term_taxonomy tt
ON tt.term_id = t.term_id
AND tt.taxonomy = 'category'
JOIN wp_term_relationships tr
ON tr.object_id = p.id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t2
-- ON ???
JOIN wp_term_taxonomy tt2
-- ON ???
JOIN wp_term_relationships tr2
-- ON ??
LEFT
JOIN wp_posts AS p2
ON p2.post_parent = p.id
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
GROUP
BY p.id
ORDER
BY p.id DESC
Where we are going to omit any join condition, and just match all rows to all other rows, then my preference is to include the (optional) CROSS keyword, as an aid the future reader, to signal that the omission of a join condition is by design, and not an oversight.

Related

Android Room inner join returns columns which are not used

I have a table for orders and customers. Customers contains an order ID and email. I wish to search for orders by customer email.
This is what I have come up with:
#RewriteQueriesToDropUnusedColumns
#Query(
"SELECT * FROM orders INNER JOIN order_customers ON order_customers.order_id = orders.id " +
"WHERE (order_customers.email LIKE '%' || :email || '%') ORDER BY orders.collection_time DESC"
)
protected abstract fun loadByEmail(email: String): LiveData<List<Order>>
I get a cursor mismatch warning "the query returns some columns which are not used" that I am loading all of the customer columns, when really all I want is orders. Adding the suggested #RewriteQueriesToDropUnusedColumns does not do anything. So what is the best syntax here? Should I just be embedding customers into the order DAO, or is there a simple solution without remodeling and migrating the data?
I can see that instead of saying SELECT * I can individually specify every order column but that is supposed to be the whole reason for Room...
You can use SELECT orders.* FROM orders INNER JOIN ....
Adding the suggested #RewriteQueriesToDropUnusedColumns does not do anything.
This could be because of the caveat:-
Note that Room will not rewrite the query if it has multiple columns that have the same name as it does not yet have a way to distinguish which one is necessary.
I'd suggest always using unique column names, doing so can avoid issues with ambiguities
Note that it appears that when there are duplicate column names, then the value of last column with the duplicated name is used by room. As can be seen in this example

Sort in GreenDAO

Raw query:
SELECT * FROM SAVED_JOB2 S, JOB J WHERE J._id=S._id ORDER BY DATE_SAVED DESC
How can I achieve sort for column in JOIN table. I have tried:
QueryBuilder<Job> queryBuilder = daoSession.queryBuilder(Job.class);
queryBuilder.join(JobDao.Properties.Id, SavedJob2.class, SavedJob2Dao.Properties.Id);
List<Job> list = queryBuilder1.list();
This normal JOIN works perfect. But I need to sort for date_saved column in table SavedJob.
I tried to add this line:
queryBuilder.orderDesc(SavedJob2Dao.Properties.date_saved);
But this line returns this error:
Property 'date_saved' is not part of com.xxx.xxx.db.JobDao
Table JobDao:
id (PK)
title
description
requirements
allowance
type
status
Table SavedJobDao:
id (PK autoincrement)
j_id (FK to JabDao)
date_saved
status
You don't need to do any JOIN with greenDao. It works with objects, so in your savedJob you should have an object Job instead job_id.
So you can order by date_saved, but searching in savedJob only, you don't need either JOIN.
There are a lot of examples. And the official doc is awesome!.

Sql search > Works on Mysql doesn't on SQLITE

recently i've started programming on android sqlite and I was debugging my sql commands with Mysql Workbench.
In one of my commands I've got this :
"Select itens.name_item , itens.pot_item from region,itens,item_regions where region.cod_region = "+id+" and itens.cod_item = item_region.cod_item;"
basically I have 3 tables.
region, itens and uniting with foreign keys those 2 inside of item_region.
What i want is by already having the region code, get the name and power of the registered itens in item_regions.
Sry if it's a little confusing, i'm very rusted on my english
Do an inner join:
SELECT name_item, pot_item FROM items
INNER JOIN item_region ON items.cod_item = item_region.cod_item
WHERE item_region.cod_region = ?;
I assume from your example you have the region code in the item_region table, otherwise you'll have to join in the region table as well to compare to the id.
Also be careful that the value of the variable id is sanitized so you don't expose your application to SQL injection.

Android SQLite query multiple tables

Im trying to do something like this (This is MySQL format)
SELECT q.question_id, q.question, q.answer
FROM relationship r, questions q
WHERE r.cat_id =1
AND r.question_id = q.question_id
LIMIT 0 , 30
So, I got questions stored in one table, then categories in another table. I have the relationship table set up so that a question can be in multiple categories. So say a question has an id of 5, and its in 3 different categories, the relationship table would look like this
relation_id, question_id, category_id
1 5 1
2 5 2
3 5 3
4 6 1
So, say I wanna get all the questions with the cat_id of 1, I should get 2 results. That's basically what I'm trying to do.
If you want all questions with cat_id of 1, then you want this:
select q.question, q.answer
from questions q
join relationship r on q.question_id = r.question_id
where r.cat_id = 1
I've switch to the ANSI join syntax rather than implied join conditions in the WHERE clause because the explicit version helps to avoid certain types of errors; in particular, ANSI joins help avoid accidental cross products and that's what your "MySQL format" query has because you neglected to include a join condition for category. That accidental cross product is almost certainly the source of your "returns each item 3 times" problem.
SELECT q.question, q.answer
FROM questions q
left join category c on q.catID = c.catID
left join relationship r on q.relID = r.relID
Some thing like this will de the trick

SQL select specific fields in JOIN query

i'm writing an Android app and i've run into a bit of a roadblock involving databases. the way Android handles databases, i cannot refer to names in the result set by the usual 'tablename.colname' method, so this presents a huge issue when any tables in the database contain the same column name. what further complicates the issue, is that any table that is used by a ViewAdapter to display the data to the user (as in my application), must contain a field named "_id" as an autoincrement primary key int. therefore, some tables MUST have identical column names. however, to avoid this, it is possible to use an "AS" clause in a statement to rename the value in question. however, i'm using a rather long statement and i don't know how to limit the columns returned on a JOINed table. what i have is this, and it's completely illegal in android due to the 'tablename.colname' references. i actually added the table names in to make the statement more readable, but i can't use them:
SELECT call._id AS android_call_id,
call.phone,
call.time,
call.duration
call.duration_billed
call.pending
call.call_id
call.job_id
FROM call
LEFT OUTER JOIN phone ON call.phone_number=phone.phone
LEFT OUTER JOIN job ON job._id=call.job_id
WHERE call.pending=1 ORDER BY job._id
but what i need, is to rename the job._id to something else using an "AS" statement, same as with the 'call._id' field in the first part of the query. how do i achieve this renaming in a JOIN?
edit:
progress so far. i think i've worked out the syntax errors, but i get another runtime error "no such column 'job._id', which may be related to #Tom H. comment
edit 2:
turns out Tom was right, and i adjusted accordingly, but it doesn't work:
SELECT call._id AS android_call_id,
call.phone,
call.time,
call.duration,
call.duration_billed,
call.pending,
call.call_id,
call.job_id,
job._id AS android_job_id,
job.job_name,
job.job_number
FROM call
LEFT OUTER JOIN phone ON call.phone_number=phone.phone
LEFT OUTER JOIN job ON job._id=call.job_id
WHERE call.pending=1 ORDER BY job._id
error:
05-24 16:50:37.561: ERROR/Minutemaid - Service(7705): oops: ambiguous column name: call._id: , while compiling: SELECT call._id AS android_call_id,call.phone_number,call.time,call.duration,call.duration_billed,call.pending,call.call_id,call.job_id,job._id AS android_job_id,job.job_name,job.job_number FROM call LEFT OUTER JOIN phone ON call.phone_number=phone.phone LEFT OUTER JOIN call ON call.job_id=job._id WHERE call.pending=1 ORDER BY job._id
Can't you simply use AS to alias all of the tablename.columnname references to unique names in the result set?
You can simply create a VIEW that restricts columns selectable in a table and assigns another name to them.
You can try massaging the table names before you join them by using sub-queries with AS in the FROM clause. For example:
select c_phone, c_id, p_id
from (select id as c_id, phone as c_phone, phone_number as c_phone_number, ... from call) as c
left outer join (select id as p_id, phone as p_phone, ... ) as p
on c_phone_number = p_phone
...
If the limitation is just that you can't use table names to distinguish between columns but can use correlation names then simpler is:
select c.id, c.phone, p.id as "p_id" from ... call c join phone p

Categories

Resources