I have the following SQL query:
SELECT p.id, title, l.id, username FROM photoTable p JOIN userTable
l ON (l.id = p.iduser) ORDER BY p.id DESC LIMIT 50
I need to change the p.id to p.IdPhoto but the problem is that I only want this done temporarily within the call. I have seen ALTER TABLE as an option, but I am a bit unsure of how I can do this without altering both id fields. Any ideas?
Would this work?
SELECT p.id as idPhoto, title, l.id,
username
FROM photoTable p
JOIN userTable l ON (l.id = p.iduser)
ORDER BY p.id DESC LIMIT 50
Related
I want the unique records against each user and product id. So my query is working fine but a small issue is, I am not getting the exact value of columns against the order id.
select max(o.id) as order_id, u.email,oi.product_id, DATE_FORMAT(FROM_UNIXTIME(o.paid_time), '%Y-%m-%d') as paid_times
from `order` o
join `users` u on u.id = o.user_id
join order_item oi on oi.order_id = o.id
where oi.product_id in (1212,1213)
group by u.email, oi.product_id
order by o.id desc;
Try - 2
select max(o.id) as order_id, u.email,oi.product_id, DATE_FORMAT(FROM_UNIXTIME(o.paid_time), '%Y-%m-%d') as paid_times
from `order` o
join `users` u on u.id = o.user_id
join order_item oi on oi.order_id = o.id
where oi.product_id in (1212,1213) and o.id IN (SELECT max(id) FROM `order`)
group by u.email, oi.product_id
order by o.id desc;
I get the max order id, which is fine. but the paid_time is not of this order id.
Current Results:
Desired Results:
This is very common type of error. for this first you need to get the max order id than use it in where condition as taking max with select and others columns mysql gives you max order id and any row data which comes first not the data related to max id.
Understand below example
SELECT name
FROM table
WHERE id=(
SELECT max(id) FROM table
)
above will give you data of max(id) from table
SELECT name , max(id)
FROM table
the above query will display Maximum id but not the matching name
http://www.plus2net.com/sql_tutorial/sql_max.php
I am working on an sql query where I have three tables posts, users and comments. I want to display all posts with its users and number of comments on this. I have following query but it is giving me wrong result:
SELECT
c.userid, count(c.userid), p.postid
FROM comments c, posts p
where c.userid = p.userid group by c.userid
In addition to above query I also require firstname and lastname from users table.
Try something like this,
SELECT
u.userid, u.firstname, u.lastname,p.post, p.postid,
count(c.userid) totalcoments -- may be c.commentid
FROM users u
JOIN posts p ON p.userid=u.userid
LEFT JOIN comments c OM c.postid=p.postid
GROUP BY u.userid, u.firstname, u.lastname,p.post, p.postid
Try something like the following:
SELECT
postid
, p.userid
, COALESCE((
SELECT COUNT( * ) FROM comments WHERE postid = p.id
), 0 ) AS cnt_postid
, COALESCE( ( SELECT CONCAT( firstname, ' ', lastname ) FROM users WHERE userid = p.userid ), 'N/A' ) AS NAME
FROM posts p
LEFT JOIN comments c ON c.postid = p.id
GROUP BY postid
ORDER BY postid
you are probably getting the same amount of count because you not using a group by.
GROUP BY must always be used when using aggregate function. What the group by does is that it will select all unique posts and the the count will return the number of users for that one unique post
THIS IS THE OUTPUT OF THE QUERY:
SELECT * FROM
((SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
privatemsgs.relatedto
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='$myid'
AND relatedto=0 and bdel=1)
UNION
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
rel.relatedto
FROM privatemsgs AS rel
JOIN privatemsgs ON(rel.relatedto = privatemsgs.id)
LEFT JOIN
users AS u ON(rel.useraid = u.id)
WHERE rel.userbid='$myid'
)) privatemsgs
GROUP BY id
ORDER BY timee DESC
I got double id "2". first 1 with "isread = 0", second with "isread = 1".
When I added "group by id", I've got (line 2)
But i need the output to show that isread = 1 (like line 3)
How do i fix it?
add MAX(isread) near the *. GROUP BY works only on Aggregate functions
I have got this query which works fine:
SELECT p.design_id, p.product_id, t.account_id, p.name, p.width, p.height,
p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users ) AS t
USING(user_id) ORDER BY p.design_id LIMIT 0, 300 );
Now I am trying to trim the result:
SELECT p.design_id, p.product_id, t.account_id, p.name, p.width,
p.height, p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users
WHERE account_id = 0) AS t
USING(user_id) ORDER BY p.design_id LIMIT 0, 3 );
But the second query result is exactly the same as the first one.
Please could somebody shine some light on this?
//UPDATE
I removed the php vaiables to stop the unnecessary confusion :)
give this a try
"SELECT p.design_id, p.product_id, t.account_id,
p.name, p.width, p.height, p.price, t.email
FROM designs AS p
INNER JOIN users AS t
ON p.user_id = t.user_ID AND
t.account_id = $ind
ORDER BY p.design_id LIMIT " . $from . ", " . $howMany
Why don't you try using a placeholder in the where condition too as like below or was it a typo.
"SELECT p.design_id, p.product_id, t.account_id, p.name, p.width, p.height, p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users WHERE account_id = **".$ind."**) AS t
USING(user_id) ORDER BY p.design_id LIMIT ".$from.", ".$howMany );
You have not selected column user_id from designs. Your JOIN is not working at all. From the manual:
The USING(column_list) clause names a list of columns that must exist in both tables.
Or all the users have account_id = 0 (or at least all the users that have designs).
If it's neither of those, try removing the word 'AS' before your nested select table 't'.
You could also try it without USING, use "ON t.user_id = p.user_id" instead of "USING(user_id)"
I'm not quite sure if this is the right approach, this is my situation:
I'm currently trying to select 15 galleries and then left join it with the user table through the id but I also want to select one random picture from each gallery however from what I know you can't limit the left join (picture) to only pick up one random picture without doing a subquery.
Here is what I got so far but its not working as it should:
SELECT galleries.id, galleries.name, users.username, pictures.url
FROM galleries
LEFT JOIN users ON users.id = galleries.user_id
LEFT JOIN pictures ON (
SELECT pictures.url
FROM pictures
WHERE pictures.gallery_id = galleries.id
ORDER BY RAND()
LIMIT 1)
WHERE active = 1
ORDER BY RAND()
LIMIT 15
I also tried to do this with Active Record but I got stuck after doing two left joins, is it possible to do get a subquery in here:
$this->db->select('galleries.id, galleries.name, users.id as user_id, users.username');
$this->db->from('galleries');
$this->db->join('users', 'users.id = galleries.user_id','left');
$this->db->join('pictures','pictures.gallery_id = galleries.id AND','left');
$this->db->where('active',1);
I hope its not to messy but I'm really starting to get confusing by all the sql queries..
Edit:
Active Record with CodeIgniter
You could fetch a random picture in a subquery:
select
g.name, u.username,
(select url from pictures p where p.gallery_id = g.gallery_id
order by rand() limit 1) as url
from galleries g
left join users u on g.user_id = u.id
where g.active = 1
Based on your comment, you could select a picture for each gallery in a subquery. This is assuming the picture table has an ID column.
select
g.name, u.username, p.url, p.name
from (
select id, user_id, name,
(select id from pictures p
where p.gallery_id = g.gallery_id
order by rand() limit 1) as samplepictureid
from galleries
where g.active = 1
) g
left join users u on g.user_id = u.id
left join pictures p on p.id = g.samplepictureid
SELECT
g.id,
g.name,
u.username,
p.url
FROM
galleries g
INNER JOIN (SELECT DISTINCT
gallery_id,
(SELECT url FROM pictures ss WHERE ss.gallery_id = s.gallery_id
ORDER BY RAND() LIMIT 1) AS url
FROM
pictures s) p ON
g.id = p.gallery_id
LEFT OUTER JOIN users u ON
g.user_id = u.id
WHERE
g.active = 1
This query will go out and select a gallery, then it will find any gallery with a picture (if you want to return galleries without a picture, change INNER JOIN to LEFT OUTER JOIN, and you'll be fine). After that, it joins it up with users. Now, of course, this puppy is going to return every frigging gallery for however many users you have (hoorah!). You may want to limit the user in the WHERE clause (e.g.-WHERE u.id = 123). Otherwise, you're going to get more results than you'd expect. That, or do an INNER JOIN on it.