I have two tables: questions, and tracking.
questions holds the columns id and url.
tracking holds the columns q_id and user_id.
What I want to do is to select a random url for all rows, except where q_id = id and where user_id is equal to the $_SESSION["id"] ($id).
Here's my query:
$results = $db->query("SELECT url FROM questions
INNER JOIN tracking ON
questions.id != tracking.q_id
WHERE tracking.user_id = '$id'
LIMIT 1 ORDER BY RAND()");
$rows = $results->fetch_array();
The query I am doing is not working for some reason, what am I doing wrong?
Following is the query which I worked for me in SQL.
SELECT top 1 url FROM Questions
INNER JOIN Tracking ON
Questions.id != Tracking.qId
WHERE tracking.UserId = 1
ORDER BY NEWID()
For MySQL, you can try "ORDER BY uuid()"
That should resolve your issue.
Thanks and regards,
Chetan Ranpariya
Related
I am currently trying to count how many dislikes and likes users have given to an image on my website, underneath you will see the database setup I did for exactly that. Vote_type keeps the like and dislike information where 1 is equal to a like, 0 is equal to a dislike.
this table is called projects
Project table
This table is called projects_votes
Now I want to include that when I make a query to drag information from my database for a blog post. This is currently the query I am making to get the necessary information to display a list of blogs on the frontpage. Here I am getting the uploader, project ID for links and so on.
$query = "
SELECT * FROM projects
LEFT JOIN users
ON fk_usr_id = usr_id
LEFT JOIN projects_maps
ON fk_project_id = project_id
ORDER BY RAND()
";
So what I have a hard time understanding is how to count my Likes and Dislikes on a specific blog, so I can display this information on my website.
I solved my problem by changing my query to this statement here.
$query = "
SELECT project_id, usr_publicname, project_name, photofile_name, map_version, usr_publicname, map_date, project_id AS project, (SELECT COUNT(*) FROM projects_votes WHERE vote_type = 1) AS likes,
(SELECT COUNT(*) FROM projects_votes WHERE vote_type = 0) AS dislikes FROM projects
LEFT JOIN projects_votes ON fk_project_id = project_id
LEFT JOIN users ON projects.fk_usr_id = usr_id
LEFT JOIN projects_maps ON projects_maps.fk_project_id = projects.project_id
LEFT JOIN photos ON photos.fk_project_id = projects.project_id
GROUP BY project_id ORDER BY RAND()
";
maybe something like this?
SELECT project_id,
sum(case when vote_type = 1 then 1 else 0 end) likeCount,
sum(case when vote_type = 0 then 1 else 0 end) dislikeCount
FROM projects
LEFT JOIN users ON fk_usr_id = usr_id
LEFT JOIN projects_maps ON fk_project_id = project_id
GROUP BY project_id
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
I am struggling with a challenging query.
I have 2 tables: posts, answers
posts has ID, PID, name
answers has ID, PID, approved
ID is the user ID, PID is the post ID.
What I am trying to do is list all the post names (posts.name) and how many unique people (IDs) have replied to their posts. The trick is that I only want to count replies that are approved (answers.approved = 'Y') AND I do not want to count the person who created the post, so (post.ID != answers.ID).
So the results should show something like:
Name People with Approved Replies
Name 1 10
Name 2 7
My current code is a mess and the wrong approach. It only gives one result and the count of replies is way off/far too high. But here is what I have currently:
$results = $dbh->prepare("select COUNT(DISTINCT answers.ID) AS reply,
posts.ID,
posts.PID,
posts.name,
answers.PID,
answers.approved
FROM answers
LEFT JOIN posts ON answers.PID=posts.PID
WHERE (answers.approved = 'Y') LIMIT 10
GROUP BY answer.ID");
$results->execute();
$row = $results->fetchAll(PDO::FETCH_ASSOC);
I think this query should do what you want
SELECT
p.name,
p.id,
p.pid,
(SELECT COUNT(DISTINCT id)
FROM answers
WHERE answers.pid = p.pid
AND answers.approved = 'Y'
AND answers.id != p.id) as reply
FROM posts p
This will select posts name , id , user id and count of distinct approved answers
I'm having difficulty understanding how to Order a query by data from another table.
The existing query is: SELECT ID FROM UserTour WHERE Live = 1 ORDER BY LastUpdated DESC
This obviously Orders by the column 'LastUpdated' in the table 'UserTour'
However, I need it to be ordered by the column 'LastUpdated' which is in another table 'ImageLinks', Where 'TypeID' = 16 (again in 'ImageLinks').
I hope that makes sense.
So it would be something like: $ids = #mysql_values('SELECT ID FROM UserTour WHERE Live = 1 ORDER BY ('Select ID FROM 'ImageLinks' Where TypeID = 16 Order by LastUpdated DESC')');
Any help would be appreciated on how to do this. Cheers
If there is no relationship between the two tables your query in your question will look like this
select id from
(
SELECT
ID
, (Select ID FROM ImageLinks Where TypeID = 16
Order by LastUpdated DESC limit 1) as order_val
FROM UserTour
WHERE Live = 1
) x
ORDER BY x.order_val
which will work but will not do not any ordering as the order_val column will have a fixed value.
If the IDs are linked 1:1 (no indication that they are, but just supposin') we could do this:
select u.id
from UserTour u inner join ImageLinks i on u.ID = i.ID
where u.Live = 1 and i.TypeID = 16
order by i.LastUpdated desc
If the above is incorrect then you will have to decide how the two tables are related and join them correspondingly.
In other words, If the tables are in no way connected, then you cannot provide an ordering of one table's data based on a column in the other.
UPDATE
select
i.LinkID
, i.LastUpdated
from UserTour u inner join ImageLinks i
on u.ID = i.LinkID
where u.Live = 1 and i.TypeID = 16
group by i.LinkID, i.LastUpdated
order by i.LastUpdated desc LIMIT 30
I have one question about JOIN from PHP section .
I know this question have in stackoverflow.com but that solution is not helped me.
I am using this code for join two table:
<?php
$select_posts = "SELECT users.uid,users.user_name,user_uploads.uid_fk,user_uploads.image_path
FROM users
JOIN user_uploads
ON users.uid = user_uploads.uid_fk
WHERE user_name='$user_name' order by rand() LIMIT 0,4";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts)) {
$image_path=$row['image_path'];
$uid_fk = $row['uid_fk'];
?>
This code will work but i want to add another table here.
other table name is: message
table inside have:
msg_id
uid_fk
comment_count
like_count
How can I add a third table here? Anyone can help me!
Just join the third table with an another JOIN keyword in your query.
SELECT table_users.uid,table_users.user_name,table_user_uploads.uid_fk,table_user_uploads.image_path, table_message.msg_id
FROM table_users
JOIN table_user_uploads
ON table_users.uid = table_user_uploads.uid_fk
JOIN table_message
ON table_message.uid_fk = table_users.uid
WHERE user_name='$user_name' order by rand() LIMIT 0,4