I have a query and i want random result on advertisement table. Advertise table have own id but when i use RAND(advertise.id) it won't work and i don't know how it will. I am using laravel framework so if possible i can use PHP also to show random advertise result. Here is code can anyone tell me where i use RAND() Mysql function.
SELECT providers.id,
adv__managements.id AS 'adv_id',
adv__managements.images,
adv__managements.categories,
adv__managements.subcategories,
adv__managements.title,
adv__managements.description,
adv__managements.role,
adv__managements.plan_id,
userinformation.user_id,
userinformation.zip_code,
plans.active
FROM userinformation INNER JOIN providers ON userinformation.user_id = providers.user_id
INNER JOIN adv__managements ON adv__managements.range = providers.range
INNER JOIN plans ON plans.user_id = userinformation.user_id
where GetDistance('km'," . doubleval($info->lat) . ", " . doubleval($info->lon) .", " . doubleval(\Auth::user()->userinfo->latitude) . ", " . doubleval(\Auth::user()->userinfo->longitude) . ") < providers.range AND plans.active = 1 LIMIT 3
And can anyone tell how to convert this query into laravel ?
Related
I am trying to fix an issue with our website and I'm afraid it is a bit out of my depth at the moment. We have a social network website and users can search for friends through a friend section.
The problem is that when you open that section the SELECT query seems to run a search for ALL users on the site. And we have over 15,000 users so the page is incredibly slow and normally times out.
I was able to limit the displayed result to 15 users per page, but the query is not well optimized and apparently selecting all users.
Here is the SELECT query code we have at the moment:
$sql_sel_friend_req = "SELECT m.*, mp.iMAgeGroup as modelAgeGroup,
c.iMAgeGroup as clientAgeGroup, mo.iModelId, mp.vImage as modelImage,
cc.vImage as clientImage FROM member m
left join model as mo on (mo.iModelId = m.MemberId && m.MemberType = 'Model')
left join model_profile as mp on (mp.iModelId = mo.iModelId)
left join client as c on (c.iClientId = m.MemberId && m.MemberType = 'Client')
left join client_company as cc on (cc.iClientId = c.iClientId) WHERE m.MemberId != " . $_SESSION['sess_Login_Id'] . " && m.MemberStatus = 'Active' &&
m.MemberId not in (SELECT distinct(mf.iFriendMemberId1) from member_friends mf where mf.iFriendMemberId2 = " . $_SESSION['sess_Login_Id'] . " && mf.eFriendMemberType2 = '" . $_SESSION['sess_Login_Type'] . "') &&
m.MemberId not in (SELECT distinct(mf1.iFriendMemberId2) from member_friends mf1 where mf1.iFriendMemberId1 = " . $_SESSION['sess_Login_Id'] . " && mf1.eFriendMemberType1 = '" . $_SESSION['sess_Login_Type'] . "') ";
I've tried to use Limit and Offset but this only caused SQL errors.
If you have any ideas please let me know.
Thank you
I am debuging php website and don't know how mysql query in it works. The code is:
$query = "
SELECT
WR.*,
W.*,
D.*
FROM
{#table} WR
LEFT JOIN words W
ON WR.word_id = W.word_id
LEFT JOIN words_descriptions WD
ON W.word_id = WD.word_id
LEFT JOIN descriptions D
ON WD.description_id = D.description_id
" . $user_words_sql . "
WHERE
title_len > 3
AND W.in_game = 1
" . $frequency_sql . "
" . $type_sql . "
GROUP BY
WR.word_id,
WR.description_id
ORDER BY
$additional_order W.frequency DESC
";
My question is where table WR is created, is it temporary table created in this peace of code or is it created somewhere else? Also, what this expression {#table} does?
The value "WR" is an alias for the query, not a temporary table. It doesn't copy the data in memory, it's just a shortcut to make the query shorter/easier to read. Here's an example:
SELECT * FROM countries c WHERE c.code = "GB"
Expands to be:
SELECT * FROM countries WHERE countries.code = "GB"
#machineaddict was correct, the {#table} has no significance in MySQL - it's likely that this is being replaced at a later stage by another part of your PHP application.
I have the following straightforward query which is part of a garbage collection script. The script is supposed to delete unused shopping carts. A cart is unused when it was updated more than 24 hours ago and is not attached to an order or a user.
$query = "SELECT comm_cart.id AS `cart_id`, (" . time() . " - comm_cart.update_date) AS `diff`, COUNT(comm_orders.cart_id) AS `c1`, COUNT(comm_users.cart_id) AS `c2` " .
"FROM `comm_cart` " .
"LEFT JOIN `comm_orders` ON comm_cart.id=comm_orders.cart_id " .
"LEFT JOIN `comm_users` ON comm_cart.id=comm_users.cart_id " .
"GROUP BY comm_cart.id ";
"HAVING `diff`>86400 AND `c1`=0 AND `c2`=0";
The query finds too many carts: it also tags the ones that have a c1>0 or c2>0 and I can't figure out why. Any clues?
I suspect that you are joining along two different dimensions. The easy fix is to use distinct:
SELECT comm_cart.id AS `cart_id`, (" . time() . " - comm_cart.update_date) AS `diff`,
COUNT(DISTINCT comm_orders.cart_id) AS `c1`, COUNT(DISTINCT comm_users.cart_id) AS `c2` " .
The better solution would be to use not exists for these two conditions:
FROM comm_carts cc
WHERE not exists (select 1 from comm_orders co where cc.id = co.cart_id )
not exists (select 1 from comm_users cu where cc.id = cu.cart_id )
You don't even need the group by, an isnull condition in the where would work wonders, of course I suggest using Gordon's suggestion of the not exists, but if you want the minimum change this would be is.
SELECT
comm_cart.id AS `cart_id`,
(UNIX_TIMESTAMP() - comm_cart.update_date) AS `diff`
FROM `comm_cart`
LEFT JOIN `comm_orders`
ON comm_cart.id=comm_orders.cart_id
LEFT JOIN `comm_users`
ON comm_cart.id=comm_users.cart_id
WHERE
comm_orders.cart_id IS NULL
AND
comm_users.cart_id IS NULL
Oh, and I've used UNIX_TIMESTAMP() instead of the PHP time function, same effect, but this avoids mixing PHP and SQL.
if you only want to get data with c1=0 and c2=0 then you need to write a where condition instead of having before group by,
$query = "SELECT comm_cart.id AS `cart_id`, (" . time() . " - comm_cart.update_date) AS `diff`, COUNT(comm_orders.cart_id) AS `c1`, COUNT(comm_users.cart_id) AS `c2` " .
"FROM `comm_cart` " .
"LEFT JOIN `comm_orders` ON comm_cart.id=comm_orders.cart_id " .
"LEFT JOIN `comm_users` ON comm_cart.id=comm_users.cart_id " .
" where c1=0 and c2 =0 and diff >86400 GROUP BY comm_cart.id;
I have 2 tables:
competition_winners where I am storing people who won competition and table competition where I am storing info about actual competition.
So I am retrieving winners and competition's end date. But the query responsible for date doesn't return anything. I am using Opencart so performing query in model. Here is its code.
public function getWinnersByDate($date) {
$qr = "SELECT competition_id FROM " . DB_PREFIX . "competition_winners";
//$fcid = $qr->row['competition_id'];
$query = "SELECT cometition_id,end_date FROM " . DB_PREFIX . "competition WHERE competition_id = '" .$qr->row['competition_id'] . "'";
return $query->row;
Query works fine in PhpMyadmin. What am I missing or doing wrong?
Instead of running this in two queries You should be using JOIN (search about SQL JOIN on google).
public function getWinnersByDate($date) {
$qr = $this->db->query("
SELECT cw.competition_id, c.end_date
FROM " . DB_PREFIX . "competition_winners cw
LEFT JOIN " . DB_PREFIX . "competition c ON c.competition_id = cw.competition_id
");
return $query->rows;
}
I do not know Your DB structure, but the query above has no sense in it's current state - I believe You want to add some WHERE clause using the provided $date argument and that You want to select more information from competition_winners table, so please, either do it Yourself or provide us with more details on Your problem.
I have this code:
select count(distinct affiliate_orders_id) as count
, sum(affiliate_value) as total
, sum(affiliate_payment) as payment
from " . TABLE_AFFILIATE_SALES . " a
left join " . TABLE_ORDERS . " o on (a.affiliate_orders_id = o.orders_id)
where a.affiliate_orders_id = o.orders_id
and o.orders_status >= " . AFFILIATE_PAYMENT_ORDER_MIN_STATUS . "
";
$affiliate_sales_query= tep_db_query($affiliate_sales_raw);
$affiliate_sales= tep_db_fetch_array($affiliate_sales_query);
So, $affiliate_sales['total'] = 128000 when in fact it should be 32000 becuase there are multiple affiliate_values and affiliate_orders_id. The affilaite_values some have the same values so these cannot be distinct. affilaite_orders_id have all unique values but there are multiple rows of this and needs to be distinct. Then the affiliate_values has to sum up based on the distinct rows of affiliate_orders_id to get an accurate sum.
I'm trying to get the sum of all affiliate_values bused on how many distinct affiliate_orders_id are in the table.
Based on your update I think this will get you what you're looking for. You need to use a subQuery
SELECT COUNT(a) COUNT, SUM(av) total, SUM(ap) aptotal
FROM (
SELECT affiliate_orders_id a, affiliate_value av, SUM(affiliate_payment) AS ap
from " . TABLE_AFFILIATE_SALES . " a
left join " . TABLE_ORDERS . " o on (a.affiliate_orders_id = o.orders_id
GROUP BY affiliate_orders_id, affiliate_value)
where a.affiliate_orders_id = o.orders_id
and o.orders_status >= " . AFFILIATE_PAYMENT_ORDER_MIN_STATUS . ") a
Now this leads to a bigger question. Are you missing a join condition in your tables? Typically you shouldn't have duplicate date returned in a query, so I would suggest double checking your query is joining properly first.