I have a query that on the left join part should either return data or null. But even if there is existing records its not returning any data. The twist to the left join portion is that I would like to only retrieve one record if it exist. Thanks for any help.
select a.*,p.thumbnailphotopath as AlbumPicture
from (select * from album_access) ac
inner join (select * from albums) a on a.ID = ac.AlbumID
left join (
select * from photos
where IsProcessed = 1 order by DateUploaded desc limit 1
) p
on a.ID = p.AlbumID #should return one if exist.
where ac.AccessUserID = '35e44a8e-643a-4c4f-8a46-59911a1e7c53'
and ac.FullControl = 1 and a.Private = 1
First you can just join on a table name and don't need to join on a whole SELECT * FROM statement.
Second, you should try not to use SELECT * and instead SELECT the columns you want.
But I think the problem with your LEFT JOIN is that you're joining on a sub-query that will only return one result, which will be the entry in photos that was last uploaded irrespective of which albumID it belongs to. If you want the entry in photos with the last uploaded date for each row, try something like this
SELECT a.*,p.thumbnailphotopath AS AlbumPicture
FROM album_access ac
INNER JOIN albums a ON a.ID = ac.AlbumID
LEFT JOIN (
SELECT albumID,MAX(DateUploaded) FROM photos
WHERE IsProcessed = 1 GROUP BY albumID
) p ON a.ID = p.AlbumID #should return one if exist.
WHERE ac.AccessUserID = '35e44a8e-643a-4c4f-8a46-59911a1e7c53'
AND ac.FullControl = 1
AND a.Private = 1
Related
with below Sql query i can get result successful when all of tables has data, but in this my query when transactions table has not any saved data and its empty my query return empty result, but i want to get null or empty columns data
SELECT transactions.id,
userEwallets.ewalletNumber,
userEwallets.currencySymbol,
transactions.money,
transactions.transactionType,
b.username AS toUser,
a.username AS sender
FROM transactions
JOIN userEwallets ON transactions.ewalletId = userEwallets.id
LEFT JOIN users AS b ON b.id = transactions.toUserId
LEFT JOIN users AS a ON a.id = transactions.fromUserId
WHERE transactions.userId = 37
when its not empty i get this result:
id ewalletNumber currencySymbol money transactionType toUser sender
95 SHIRR9373036569 IRR 20 1 1 amin
you can use a dummy table with one row. The other tables should be left joined to it.
SELECT transactions.id,
userEwallets.ewalletNumber,
userEwallets.currencySymbol,
transactions.money,
transactions.transactionType,
b.username AS toUser,
a.username AS sender
FROM (select 1) dummy
LEFT JOIN transactions ON transactions.userId = 37
LEFT JOIN userEwallets ON transactions.ewalletId = userEwallets.id
LEFT JOIN users AS b ON b.id = transactions.toUserId
LEFT JOIN users AS a ON a.id = transactions.fromUserId
You can allways use a subquery instead of a table name if you give it an allias. Note that you have to move the WHERE condition for the left joined table into the ON clause - Othewise MySQL will convert it to an INNER JOIN.
You are using where condition here. where transactions.userId = 37. If there is no data in transactions table, that where condition will fail and hence returns no data
In order to accomplish that, you should use another table to be the first one. The way you are doing it, if there are no records in the transactions table then there would be nothing to join with.
So, use a table that always has records and LEFT JOIN it with the others.
Try this:
SELECT transactions.id,
userEwallets.ewalletNumber,
userEwallets.currencySymbol,
transactions.money,
transactions.transactionType,
b.username AS toUser,
a.username AS sender
FROM users AS b
LEFT JOIN transactions ON b.id = transactions.toUserId
LEFT JOIN users AS a ON a.id = transactions.fromUserId
JOIN userEwallets ON transactions.ewalletId = userEwallets.id
WHERE transactions.userId = 37
I try to do an INNER JOIN depending of the result of CASE.
SELECT *
FROM album
INNER JOIN (
SELECT album_type CASE album.album_type
WHEN 1 THEN "album_int"
WHEN 2 THEN "album_ext"
END FROM album)
AS type ON type.album_id = album.id
WHERE album.id = 6
LIMIT 1;
I have see some examples on this site, but nothing work in my case.
Someone can help me?
I have 3 tables (album, album_int and album_ext).
I want to join album_int or album_ext to album.
if album.album_type = 1 I join album_int,
else if album.album_type = 2 I join album_ext.
In album_int and album_int I have a column name album_id (same unique
id in album)
You do not need any case expressions or even a where clause.
SELECT a.`id`, COALESCE(aint.`something`, aext.`something`) as something, ...
FROM ALBUM A
LEFT OUTER JOIN ALBUM_INT AS AINT ON A.ID = AINT.ALBUM_ID AND A.ALBUM_TYPE = 1
LEFT OUTER JOIN ALBUM_EXT AS AEXT ON A.ID = AEXT.ALBUM_ID AND A.ALBUM_TYPE = 2
AND A.ALBUM_TYPE = 1 as a join condition means that ALBUM_INT will ONLY join to ALBUM rows where album_type = 1
similarly:
AND A.ALBUM_TYPE = 2 as a join condition means that ALBUM_EXT will ONLY join to ALBUM rows where album_type = 2
please note that an answer previously given by Gordon Linoff uses the same join logic, I have just attempted to emphasize how it meets the described requirements.
case doesn't work on tables. You can use left join:
SELECT *
FROM album a LEFT JOIN
album_int ai
ON ai.album_id = a.id AND a.album_type = 1 LEFT JOIN
album_ext ae
ON ae.album_id = a.id AND a.album_type = 2
WHERE a.id = 6 AND a.album_type IN (1, 2)
LIMIT 1;
To get values into the same column, you then need to use coalesce().
Note: This does the right thing if there is at most one match in each table.
To Join to one of two tables conditionally, create a join column on each table and UNION them together as such:
SELECT *
FROM albums a
INNER JOIN (
SELECT 1 AS album_type, album_id, colA, colB
FROM album_int
UNION ALL
SELECT 2 AS album_type, album_id, colA, colB
FROM album_ext ) AS b
ON a.album_type = b.album_type
AND a.album_id = b.album_id
Is there anyway to select all from 1 table based on the result of one query which contains multiple values without having to do 2 separate queries?
Say long join query will produce a list of id's
SELECT xyz FROM table long join query WHERE id = array of ids found in result table
added example:
SELECT * FROM tweets as t
where t.user_id
in(
SELECT uff.id, uff.username
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)
The bit in the parenthesis returns an id and user name of who the user is following (uff.id=1)
How do i then get all 'tweets' by all the id's in the generated resultset
You can use subquery:
SELECT * FROM `table1` WHERE `id` IN (SELECT `table2`.id FROM `table2` )
You might want to check documentation for syntax
SELECT xyz FROM table_A join table_B WHERE table_A.id IN (SELECT ID FROM table_C);
I think you mean something like
edited after the OP edit
SELECT * FROM tweets as t
WHERE t.user_id
in(
SELECT uff.id //remove the second field, you just need the id
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)
After trying the in clause I coudnt get the results I was after but after rethinking what I was trying to do I got my results with an additional join clause
SELECT uff.username, t.content
FROM users as uf
JOIN followlinks as fl
ON uf.id = fl.user_id
JOIN users as uff
ON fl.follow_id = uff.id
JOIN tweets as t
ON t.user_id = uff.id
WHERE uf.id = 1
I have 5 tables: a, b, c, d and e.
Each table is joined by an INNER JOIN on the id field.
My query is working perfectly fine as it is but I need to enhance it to count the result so I can echo it to the screen. I have not been able to get the count working.
There are very specific fields I am querying:
state_nm
status
loc_type
These are all parameters I enter manually into the query like so:
$_POST["state_nm"] = 'AZ'; ... // and for all other below values..
SELECT *
FROM main_table AS a
INNER JOIN table_1 AS b ON a.id = b.id
INNER JOIN table_2 AS c ON b.id = c.id
INNER JOIN blm table_3 AS d ON c.id = d.id
INNER JOIN table_4 AS e ON d.id = e.id
WHERE a.trq != ''
AND b.state_nm = '".$_POST["state_nm"]."'
AND b.loc_type LIKE \ "%".$_ POST ["loc_type"]."%\"
AND b.STATUS = '".$_POST["status"]."'
GROUP BY b.NAME
ORDER BY c.county ASC;
not sure I get exactly what is your goal here.
anyway, using "select *" and group by in the same query is not recommended and in some databases will raise an error
what I would do is something like that:
select a.name, count(*) from (
SELECT * FROM main_table as a
INNER JOIN table_1 as b
ON a.id=b.id
INNER JOIN table_2 as c
ON b.id=c.id
INNER JOIN blm table_3 as d
ON c.id=d.id
INNER JOIN table_4 as e
ON d.id=e.id
WHERE a.trq != ''
AND b.state_nm = '".$_POST["state_nm"]."'
AND b.loc_type LIKE \"%".$_POST["loc_type"]."%\"
AND b.status = '".$_POST["status"]."'
)a
group by a.name
the basic idea is to add an outer query and use group by on it...
hopefully this solves your problem.
In place of
SELECT *
in your query, you could replace that with
SELECT COUNT(*)
That query should return the number of rows that would be in the resultset for the query using SELECT *. Pretty easy to test, and compare the results.
I think that answers the question you asked. If not, I didn't understand your question.
I didn't notice the GROUP BY in your query.
If you want to get a count of rows returned by that query, wrap it in outer query.
SELECT COUNT(1) FROM (
/* your query here */
) c
That will give you a count of rows returned by your query.
I would like to get the data from one table, and count all results from other table, depending on the first table data, here is what I tried:
SELECT
cars.*, (
SELECT
COUNT(*)
FROM
uploads
WHERE
uploads.cid = cars.customer
) AS `count`,
FROM
`cars`
WHERE
customer = 11;
I dont really have an idea why its not working, as I'm not a regular MySQL user/coder...
Could anyone direct me in the right direction with this one?
SELECT
c.*, COUNT(u.cid) AS count
FROM
cars c
LEFT JOIN
uploads u
ON
u.cid=c.customer
WHERE
u.customer = 11;
GROUP BY c.cid
Try it by joining both tables using LEFT JOIN
SELECT a.customer, COUNT(b.cid) totalCount
FROM cars a
LEFT JOIN uploads b
ON a.customer = b.cid
WHERE a.customer = 11
GROUP BY a.customer
using COUNT(*) in LEFT JOIN will have records to have a minimum count of 1.
SELECT cars.*,COUNT(uploads.*) as uplloaded
from cars
left outer join uploads on uploads.cid = cars.customer
where cars.customer = 11
group by uploads.cid;
Try this :
SELECT customer, COUNT(cid) totalCount
FROM cars
INNER JOIN uploads
ON (customer = cid)
WHERE customer = 11
GROUP BY customer