I have this query that creates a list of 10 names that are in my facebookfriends list that are not already in my contact list, but I want it to randomly select these 10 names.
I can't get the other examples to work with my query, please can you help me get this right.
Here is the sql:
SELECT TOP 10 FaceBookFriends.FaceBookName, FaceBookFriends.FullName,
FaceBookFriends.FirstName, FaceBookFriends.Surname,
FaceBookFriends.DateAdded
FROM FaceBookFriends LEFT JOIN Contact_List
ON FaceBookFriends.[FullName] = Contact_List.[Full _Name]
WHERE (((Contact_List.[Full _Name]) Is Null));
I added a ORDER BY Rnd(FaceBookFriends.FullName) at the end of the report but it doesn't work.
Try this in order by clause:
order by newid()
Related
I am facing some groupby aggregation issue in my below join query:
SELECT chat_tbl.chatrec_id,
chat_tbl.senderid,
chat_tbl.receiverid,
chat_tbl.msg,
chat_tbl.chat_time,
chat_tbl.chatuser_strid,
chatuser_link_tbl.str_id,
chatuser_link_tbl.modifytime
FROM chat_tbl
INNER JOIN chatuser_link_tbl ON chat_tbl.chatuser_strid = chatuser_link_tbl.str_id
AND (chat_tbl.senderid=393 OR chat_tbl.receiverid=393)
GROUP by chat_tbl.chatuser_strid
ORDER by chatuser_link_tbl.modifytime DESC
I know I can disable the only_full_group_by setting by executing the following:
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
But that is the wrong method it will waive off the settings and this is again removed after every server restart so i want to correct my query. Please anyone can help me to correctly write this join query from 3 tables.
Thanks in advance.
Update 1: I am using this query to list all the chat done by the current user with other users.
I have 2 tables to do that chatuser_link_tbl has the link of the chat done between 2 users for example 11-18, 12-22 i.e concatenated 2 user id with dash - and chat table has an actual chat data.
I am using group by to group all the chats using chatuser_strid to get only single last record. I am displaying only last chat message with date time.
In short this is the same as whatsapp chat list (the first home screen) or like facebook messenger chat list. Which display user details with their last chat message.
Since your query is designed to work on a single user (sender or receiver equal to 393) you can simply drop the group by and add a limit
SELECT chat_tbl.chatrec_id,
chat_tbl.senderid,
chat_tbl.receiverid,
chat_tbl.msg,
chat_tbl.chat_time,
chat_tbl.chatuser_strid,
chatuser_link_tbl.str_id,
chatuser_link_tbl.modifytime
FROM chat_tbl
INNER JOIN chatuser_link_tbl ON chat_tbl.chatuser_strid = chatuser_link_tbl.str_id
AND (chat_tbl.senderid=393 OR chat_tbl.receiverid=393)
ORDER by chatuser_link_tbl.modifytime DESC
LIMIT 1
I think you want to use window functions for this:
SELECT *
FROM (SELECT c.*, cl.str_id, cl.modifytime,
ROW_NUMBER() OVER (PARTITION BY c.chat_user_strid ORDER BY cul.modifytime DESC)
FROM chat_tbl c JOIN
chatuser_link_tbl cul
ON c.chatuser_strid = cul.str_id AND
393 IN (c.senderid, c.receiverid)
) c
WHERE seqnum = 1;
I have fixed the issue myself. Below query worked for me to get desired result.
ANY_VALUE helped me.
SELECT chat_tbl.chatuser_strid, ANY_VALUE(chat_tbl.chatrec_id),ANY_VALUE(chat_tbl.senderid),ANY_VALUE(chat_tbl.receiverid),ANY_VALUE(chat_tbl.msg),ANY_VALUE(chat_tbl.chat_time), ANY_VALUE(chatuser_link_tbl.str_id)
FROM chat_tbl
INNER JOIN chatuser_link_tbl
ON chat_tbl.chatuser_strid = chatuser_link_tbl.str_id AND (chat_tbl.senderid=".$userid." OR chat_tbl.receiverid=".$userid.")
GROUP by chat_tbl.chatuser_strid
ORDER by ANY_VALUE(chatuser_link_tbl.modifytime) DESC
I keep getting the same error: But as far as I can tell it is included in the Query:
SELECT TOP 1 tblOutlets.OutletName, tblOutlets.Parish, Count([Query OOSActions].OODate) AS CountOfOODate1
FROM tblOutlets INNER JOIN [Query OOSActions] ON tblOutlets.OutletID = [Query OOSActions].OutletLookup
WHERE ((([Query OOSActions].OODate) Between [Forms]![Settings]![StartDate] And [Forms]![Settings]![EndDate]) AND (([Query OOSActions].Classification)="Biscuit"))
GROUP BY tblOutlets.OutletName, tblOutlets.Parish
HAVING (((tblOutlets.Parish)="St. Mary"))
ORDER BY Count([Query OOSActions].OODate) DESC;
UNION
SELECT TOP 1 tblOutlets.OutletName, tblOutlets.Parish, Count([Query OOSActions].OODate) AS CountOfOODate1
FROM tblOutlets INNER JOIN [Query OOSActions] ON tblOutlets.OutletID = [Query OOSActions].OutletLookup
WHERE ((([Query OOSActions].OODate) Between [Forms]![Settings]![StartDate] And [Forms]![Settings]![EndDate]) AND (([Query OOSActions].Classification)="Biscuit"))
GROUP BY tblOutlets.OutletName, tblOutlets.Parish
HAVING (((tblOutlets.Parish)="St. Catherine"))
ORDER BY Count([Query OOSActions].OODate) DESC;
I solved it by including ORDER BY only in the first query and then removing all ending semicolons. Thanks for that bit #Uueerdo
When using "TOP" and "ORDER BY" in the second SQL statement of a UNION query, I have experienced that it runs the TOP instruction first and then the "ORDER BY", so it makes no sense to order by anything here. With a simpler SQL example:
SELECT TOP 1 Ingresos.ID_Fra, Ingresos.Tipo, Ingresos.Numero, Ingresos.Fecha, "First Query" AS Tbl
FROM Ingresos
ORDER BY Ingresos.Fecha DESC
UNION
SELECT TOP 5 IngresosAnulados.ID_Fra, IngresosAnulados.Tipo, IngresosAnulados.Numero, IngresosAnulados.Fecha, "DEL" AS Tbl
FROM IngresosAnulados
ORDER BY Fecha DESC;
Source table: IngresosAnulados:
Assuming that the data in "IngresosAnulados" are these 10 records, the defined SQL statement shows the following result:
As you can see, it has taken five records from the second table and has ordered them by the field "Fecha", as specified, but we do not know what the criteria are for having selected the five records that it has chosen.
By the way, it is necessary to indicate the name of a field selected in the first query as the order of any of the union queries (except the first one). That's just what the error message says (The ORDER BY expression includes fields that are not selected by the query).
So you will need two independent queries (at least, an independent query for the second SQL statement) to get the record(s) that interests you in each of them and then join them in a UNION query if you really want the x top most of each UNION query to be displayed.
I have many categories with the same name and parent in my Opencart database (duplicates). Need to find all of them. That's my query:
SELECT *
FROM
(SELECT `oc_category`.category_id,
`oc_category`.parent_id,
`oc_category_description`.name
FROM `oc_category`, `oc_category_description`
WHERE `oc_category`.category_id = `oc_category_description`.category_id
) cats
GROUP BY `cats`.parent_id, `cats`.name
HAVING COUNT(*) > 1
But this query returns nothing. Please tell me if I'm wrong.
No problem with the query, it does work, check this out:
http://sqlfiddle.com/#!9/3d170/4
Please fiddle with that and populate it with the data which produces no records, and add it to your question.
I have the following query:
SELECT routes.route_date, time_slots.name, time_slots.openings, time_slots.appointments
FROM routes
INNER JOIN time_slots ON routes.route_id = time_slots.route_id
WHERE route_date
BETWEEN 20140109
AND 20140115
AND time_slots.openings > time_slots.appointments
ORDER BY route_date, name
This works just fine and will produce the following results:
What I want to do is only return one name per date. So the 9th, name = 1, would only have 1 result, rather than 2, as it currently does.
UPDATE: See the SQLFIDDLE for different type of solutions here: http://sqlfiddle.com/#!2/9ac65b/6
Will it solve your request if you use...
SELECT DISTINCT routes.route_date...your query... ?
It depends if you know that your rows always will have the same values, for same date/name.
Otherwise use group by...
(which I think suits your request best)
SELECT routes.route_date, time_slots.name, sum(time_slots.openings), sum(time_slots.appointments)
FROM routes
INNER JOIN time_slots ON routes.route_id = time_slots.route_id
WHERE route_date
BETWEEN 20140109
AND 20140115
AND time_slots.openings > time_slots.appointments
group by routes.route_date, time_slots.name
ORDER BY route_date, name
(i did a sum for the openings and appointments, you could do min, max, count, etc. Pick the one that fits your requirements best!)
You need to figure out which "name" you want when there are several for the same date.
Then you can group by date and select the right "name" by using an aggregate function like COUNT, MAX, etc.
I can't help you more if you don't explain your rule for picking one.
MySQL Server Version: Server version: 4.1.14
MySQL client version: 3.23.49
Tables under discussion: ads_list and ads_cate.
Table Relationship: ads_cate has many ads_list.
Keyed by: ads_cate.id = ads_list.Category.
I am not sure what is going on here, but I am trying to use COUNT() in a simple agreggate query, and I get blank output.
Here is a simple example, this returns expected results:
$queryCats = "SELECT id, cateName FROM ads_cate ORDER BY cateName";
But if I modify it to add the COUNT() and the other query data I get no array return w/ print_r() (no results)?
$queryCats = "SELECT ads_cate.cateName, ads_list.COUNT(ads_cate.id),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName ORDER BY cateName";
Ultimately, I am trying to get a count of ad_list items in each category.
Is there a MySQL version conflict on what I am trying to do here?
NOTE: I spent some time breaking this down, item by item and the COUNT() seems to cause the array() to disappear. And the the JOIN seemed to do the same thing... It does not help I am developing this on a Yahoo server with no access to the php or mysql error settings.
I think your COUNT syntax is wrong. It should be:
COUNT(ads_cate.id)
or
COUNT(ads_list.id)
depending on what you are counting.
Count is an aggregate. means ever return result set at least one
here you be try count ads_list.id not null but that wrong. how say Myke Count(ads_cate.id) or Count(ads_list.id) is better approach
you have inner join ads_cate.id = ads_list.category so Count(ads_cate.id) or COUNT(ads_list.id) is not necessary just count(*)
now if you dont want null add having
only match
SELECT ads_cate.cateName, COUNT(*),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
having not count(*) is null
ORDER BY cateName
all
SELECT ads_cate.cateName, IFNULL(COUNT(*),0),
FROM ads_cate LEFT JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
ORDER BY cateName
Did you try:
$queryCats = "SELECT ads_cate.cateName, COUNT(ads_cate.id)
FROM ads_cate
JOIN ads_list ON ads_cate.id = ads_list.category
GROUP BY ads_cate.cateName";
I am guessing that you need the category to be in the list, in that case the query here should work. Try it without the ORDER BY first.
You were probably getting errors. Check your server logs.
Also, see what happens when you try this:
SELECT COUNT(*), category
FROM ads_list
GROUP BY category
Your array is empty or disappear because your query has errors:
there should be no comma before the FROM
the "ads_list." prefix before COUNT is incorrect
Please try running that query directly in MySQL and you'll see the errors. Or try echoing the output using mysql_error().
Now, some other points related to your query:
there is no need to do ORDER BY because GROUP BY by default sorts on the grouped column
you are doing a count on the wrong column that will always give you 1
Perhaps you are trying to retrieve the count of ads_list per ads_cate? This might be your query then:
SELECT `ads_cate`.`cateName`, COUNT(`ads_list`.`category`) `cnt_ads_list`
FROM `ads_cate`
INNER JOIN `ads_list` ON `ads_cate`.`id` = `ads_list`.`category`
GROUP BY `cateName`;
Hope it helps?