I am having only one record under the cust_vend_relation
But its being displayed two times.
QUERY
select cvr.customer_id,
cvr.address_label,
cvr.vendor_name,
cvr.vendor_id,
vhd.locality,
vhd.area,
vhd.address,
vhd.city,
vhd.state
from cust_vend_relation cvr, vendor_home_delivery vhd
where cvr.vendor_id = vhd.vendor_id
and cvr.address_label = 'Office'
and cvr.customer_id = 3;
This is my sqlfiddle
I need to show only the records present under the table cust_vend_relation that matches with the records present under the vendor_home_delivery.
could anybody please help me .
Add Group By customer_id
select
cvr.customer_id,
cvr.address_label,
cvr.vendor_name,
cvr.vendor_id,
vhd.locality,
vhd.area,
vhd.address,
vhd.city,
vhd.state
from cust_vend_relation cvr, vendor_home_delivery vhd
where cvr.vendor_id = vhd.vendor_id and cvr.address_label = 'Office' and cvr.customer_id = 3 group by customer_id;
Related
Please help me to get three records in a single column. Please find the query below and help me. Thanks in advance
SELECT
salereg.currency_symbol AS currency_symbol_1,
salereg.company_name AS company_name_2,
salereg.customer_type_descr AS customer_type_descr_3,
salereg.product_name AS product_name_4,
salereg.priceline_descr AS priceline_descr_5,
salereg.charge_amount AS charge_amount_6,
salereg.discount_amount AS discount_amount_7,
salereg.adjustment_amount AS adjustment_amount_8,
salereg.tax_amount AS tax_amount_9
FROM
sales_register_summary_vw salereg,
customer_details ci,
user_entities ue
WHERE
trunc(salereg.invoice_date) BETWEEN to_date(:p_frminvoice_date, 'dd/MM/yyyy') AND to_date(:p_toinvoice_date, 'dd/MM/yyyy')
AND salereg.opentity = :p_opentity
AND salereg.customer_type = :p_customer_type
AND salereg.customer_id = ci.customer_id
AND ci.opentity_id = ue.entity_id
AND ue.is_deleted = 0
AND ue.user_id = :p_userid
GROUP BY
salereg.currency_symbol,
salereg.company_name,
salereg.customer_type_descr,
salereg.product_name,
salereg.priceline_descr,
salereg.charge_amount,
salereg.discount_amount,
salereg.adjustment_amount,
salereg.tax_amount;
I would like to get lowest price of product based on last crawled dates by various resellers. My current function is very basic, it gets me lowest price from table without considering reseller ids and crawled timestamps.
I've rough idea that we can SELECT * FROM "custom_data_table" and process the data using php. Please have a look at attachment for further clarification.
function get_lowest_price($table_id) {
global $wpdb;
$table_prices = $wpdb->get_results(
$wpdb->prepare(
"SELECT price FROM `custom_data_table` WHERE tableid= %d"
,$table_id)
);
if (!empty($table_prices) && $table_prices !== NULL)
return rtrim(min($table_prices)->price, '00');
}
The right query here is:
SELECT price
FROM custom_data_name cdn, (
SELECT MAX(crawled) AS maxCrawled, resellerid
FROM custom_data_name
GROUP BY resellerid
) cdnFiltered
WHERE cdn.crawled = cdnFiltered.maxCrawled AND
cdn.resellerid = cdnFiltered.resellerid AND
tableid = %d;
Try this:
SELECT B.price
FROM (SELECT resellerid, MAX(crawled) max_crawled
FROM custom_data_table
GROUP BY resellerid) A
JOIN custom_data_table B
ON A.resellerid=B.resellerid AND A.max_crawled=B.crawled;
Maybe use ORDER BY crawled and LIMIT 1
I'm migrating a project to Laravel 4 and I am stuck with a quite complex query, which I'd like to migrate into a proper Eloquent query.
I have a table that contains chat messages, called chat_messages with a representing Model Chatmessage
The table contains a sender and a receipient column with a user id linking to the users table and User Model.
The query to get a list with all user IDs of all chat partners in raw SQL on the old version of the application is as follows:
$sql_allChatPartners = "SELECT DISTINCT chatPartner
FROM ( SELECT * FROM (
SELECT cm_receipient AS chatPartner, cm_sent_at
FROM chat_messages WHERE cm_sender = '".$me->userID."'
UNION
SELECT cm_sender AS chatPartner, cm_sent_at
FROM chat_messages WHERE cm_receipient = '".$me->userID."'
) whateva ORDER BY whateva.cm_sent_at DESC ) other";
Sorry for naming the "fake" tables whateva and other :-)
Could anyone put me in the right direction to do this with Eloquent Querybuilder?
It is important that I get the list of chatPartner IDs in the correct order, where the last chat message has been exchanged as first chatPartner. And the chatPartner where longest inactivity was in the chat as last entry.
This is what I got so far in my User Model...
public function allopenchats(){
$asSender = Chatmessage::where('sender', $this->id)->select('receipient as chatPartner, created_at');
$asBoth = Chatmessage::where('receipient', $this->id)->select('sender as chatPartner, created_at')
->union($asSender)->orderBy('created_at', 'desc')->get();
}
I renamed the columns cm_receipient to receipient, cm_sender to sender and sent_at to created_at in the new database for the new version
Your help would be very much appreciated!
You sql may change to:
SELECT IF (cm_receipient = '10', cm_sender, IF (cm_sender = '10',cm_receipient, 'no')) AS chatPartner, cm_sent_at
FROM chat_messages
WHERE cm_receipient = '10' OR cm_sender = '10'
GROUP BY chatPartner
HAVING chatPartner != 'no'
order by cm_sent_at DESC
In orm:
Chatmessage::where('sender','=',$this->id)
->orWhere('receipient','=',$this->id)
->select(DB::raw('IF (receipient = '.$this->id.', sender, IF (sender = '.$this->id.',receipient, 'no' )) AS chatPartner'), 'created_at')
->groupBy('chatPartner')
->having('chatPartner', '!=', 'no')
->orderBy('created_at', 'desc')
->get();
Thanks very much to Vitalik_74, I wouldn't have come that far without him.
Here is now the final query, although its not in ORM, but it is working fine and gives me the result I need.
$result = DB::select("SELECT *
FROM (
SELECT IF( receipient = '".$this->id."', sender, IF( sender = '".$this->id."', receipient, 'no' ) ) AS chatPartner, created_at
FROM chatmessages
WHERE receipient = '".$this->id."'
OR sender = '".$this->id."'
HAVING chatPartner != 'no'
ORDER BY created_at DESC
)whateva
GROUP BY whateva.chatPartner
ORDER BY whateva.created_at DESC");
if there is someone out there who can do this query with the Laravel Query Builder, I would be happy to see it. For now I'll leave it like this.
I need to fill some fields in a table getting informations from other records of the same table.
I tried to write a query to explain what I want to do:
update globale2
set
nita = t.nita,
tita = t.tita,
notaita = t.notaita
where
neng = t.neng and
nita is null
(select nita, neng, tita, notaita from globale where uris='mma' and nita is not null) as t
edit to eplain better:
every records have these fields: "nita", "tita", "notaita", "neng" ("neng" cannot be null)
I want to fill these fields: "nita", "tita", "notaita" (where "nita" is empty)
with the same values from another record where "neng" equals the other "neng"
You can however, join the two tables.
UPDATE globale2 g
INNER JOIN globale gg
ON g.neng = gg.neng
SET g.nita = gg.nita,
g.tita = gg.tita,
g.notaita = gg.notaita
WHERE g.nita IS NULL
AND gg.uris = 'mma'
AND gg.nita IS NOT NULL
assume there is a table A_temp, with two columns 'one' and 'two'.
TABLE A_temp
ONE TWO
1 2
this is the present status of the table.
The query
UPDATE (SELECT * FROM A_temp ) A SET one = A.two where one = '1'
updates the table as
ONE TWO
2 2
Hope you get the idea and that it helps..
$slt = mysql_query("select Slot, ItemId, UserId, max(Slot) Slot
from useritems
group by UserId");
while ($sloot = mysql_fetch_assoc($slt))
{
Echo "<br>Items with biggest slots are: " . $sloot['ItemId'] . " from user " . $sloot['UserId']. "- in slot-". $sloot['Slot'];
}
This is the table
Idi Quantity ItemId UserId Slot ExpirationDate
Outputs the smallest Slots...Why?
1.I want to show me the biggest inventory slot from each user, so when i add a new item to his inventory i can add on next slot..From example user Paul has 5 items that ocupies slots 1,2,3,4,5 and the next item will be on slot 6.
2.When a user moves his items on slots 1,2,4,5,6 the next item added will be on slot 3
I did a lot of search but i can't find out myself:) PS:The game wich im making its just for fun..but maybe someday will be a great game:) (dreams,dreams :)) )
EDIT:
SQLFIDDLE is very good thank you:) it's exactly what i need to learn some SQL
Table useritems
useritems Table IMAGE
items Table
My Echo shows me that:
Id = 1 and it should be 3; user=4(good); slot=4(good)
Id = 1 and it should be 2; user=5(good); slot=2(good)
Maybe something like:
select userid, itemid, max(slot) from useritems where itemid is not null and quantity>0
group by userid, itemid
It'd be easier to help you if you share your table script with some data.
You can use: http://sqlfiddle.com/
EDIT:
What about this? Itemid=0 means the slot is free? So min(slot) will be the first free slot by user.
select userid, min(slot) from useritems where itemid=0 group by userid
I found it!:) i just forgot to select data where UserId=my_id but it shows me the corect output only if the respective user has more than 3 items...
$slt = mysql_query("SELECT * FROM useritems WHERE Slot=(select max(Slot) from useritems) and UserId='$id'");
and $id = $_SESSION['id'];
$slt = mysql_query("
SELECT * FROM `useritems`
WHERE UserId='$id' AND Slot=(select max(Slot)
from useritems where UserId='$id')
");`
EDIT2:I found the best way with all i was searching but i don`t knwo how to use WHERE clause userId='$id'
$slt2 = mysql_query("select l.Slot + 1 as start
from useritems as l
left outer join useritems as r on l.Slot + 1 = r.Slot
where r.Slot is null and l.UserId=4;
") or die(mysql_error()); `
With this query the item should be placed on Slot 2(wich is missing) but it`s dysplays Slot 6 (wich is the highest for the user with UserId=4)
Finally this is the last edit
$slt2 = mysql_query("SELECT Slot + 1 as start
FROM useritems mo
WHERE NOT EXISTS
(
SELECT NULL
FROM useritems mi
WHERE mi.Slot = mo.Slot + 1 AND mi.UserId = '$id'
)ORDER BY Slot LIMIT 1
") or die(mysql_error()); `
This is what i was searching.:X