$insert_sql = "INSERT INTO exported_leads (lead_id, partner_id) VALUES ('$id','$partner_id')
ON DUPLICATE KEY UPDATE
lead_id = VALUES(lead_id), partner_id = VALUES(partner_id), export_date = CURRENT_TIMESTAMP";
$count = $pdo->exec($insert_sql);
$count_total = $count_total + $count;
i have this code. problem is that whenever there's an existing match in the db it overwrites the partner_id with a new value. i need to keep which one exported the row when. i was thinking about adding columns to cover those, but the problem with that is i dont know how many exporters there can be. sometimes it can be 5, sometimes it can be over 20.
i currently use this query to find what i should export:
$sql = $pdo->query('SELECT p.* FROM prospects p
LEFT JOIN exported_leads e
on p.id = e.lead_id WHERE p.partner_id != '.$partner_id.' AND (e.lead_id IS NULL OR datediff(now(), e.export_date) > 90)
LIMIT '.$monthly_uttag.'');
and this also shows why i cant just paste new rows in the already exported db. because it keeps finding the first record. not the newest entry first. which screws it up.
anyhow, how would you do this and still keep a date when every partner_id exported that row?
say for example the row id=1. partner1 exported that row on 2014-01-01 and partner3 exported it on 2014-05-15. i need to keep both those dates somehow.
how would you do this? im looking for an indefinitely large possible amount of partners
Related
Basically I have a rating website where users rate other people from different schools. Right now I'm trying to do the leaderboard script but I cannot properly get the rating to show. My query is only returning one result. ($helpMe) Essentially I'm declaring the average total since my db doesn't store that, it only stores total number and total votes and divides accordingly.
https://pastebin.com/fVf4B8En here is the main code
I suspect that the culprit is here but not sure how
$helpMe = mysql_query("SELECT `rating_number`, FORMAT( (`total_points` / `rating_number`), 1 ) AS `average_rating` FROM `view_rating` WHERE `status` = 1 ORDER BY `average_rating` DESC");
while ($ratingRow = mysql_fetch_assoc($helpMe)) {
I have a database where two of the tables have columns called Building Code. It is set as VARCHAR(5) in both of them, and sometimes the value is purely digits (ex: 0001) and sometimes it has a letter in it (ex: 0170A)
The two different tables in this database, are called "buildings", and "newconnections", of which I am trying to perform an inner join on.
In one of the tables, when I search for 0001 in phpmyadmin I get no results, but if I search for 0170A I get the correct results. In the other table, both work correctly. This issue has been affecting me as I have been trying to do an inner join on these data sets.
I have tried using LTRIM, RTRIM (this did not work), I tried casting as VARCHAR(5) (although admittedly this query just wasn't producing results so I may had done it wrong).
Here is the inner join I am trying to perform if it helps:
"SELECT BUILDING_CODE, buildings.BuildingName, buildings.Latitude, buildings.Longitude, FLOOR1, FLOOR2, FLOOR3
FROM buildings LEFT JOIN
(SELECT BUILDING_CODE,
SUM(IF(FLOOR_NUMBER = '1', FLOORCONNECTIONS, 0) ) AS FLOOR1,
SUM(IF(FLOOR_NUMBER = '2', FLOORCONNECTIONS, 0) ) AS FLOOR2,
SUM(IF(FLOOR_NUMBER = '3', FLOORCONNECTIONS, 0) ) AS FLOOR3
FROM
(
SELECT
BUILDING_CODE, FLOOR_NUMBER, COUNT(*) AS FLOORCONNECTIONS
FROM newconnections GROUP BY BUILDING_CODE, FLOOR_NUMBER
) as totals
GROUP BY BUILDING_CODE
) as r ON RTRIM(BUILDING_CODE) = RTRIM(buildings.BuildingCode)";
For instance, on the table that is giving me troubles:
SELECT * FROM newconnections WHERE BUILDING_CODE = '0170A'
returns results but
SELECT * FROM newconnections WHERE BUILDING_CODE = '0001'
returns nothing.
On the working table:
SELECT * FROM newconnections WHERE BuildingCode= '0170A'
and
SELECT * FROM newconnections WHERE BuildingCode= '0001'
return results
EDIT: A little more information. I actually generated a lot of this data using a script that I created and was inserting it into a text file and then inserting it into a database from that file. I guess there must be some weird issue with how it was printing certain numbers - I put my data this time into excel and exported it into a table from there and I added that table to my database and the queries run fine now, so case closed (although I wish I could figure out how to actually fix this problem for future references).
I try to use different columns within different tables.
Like I want it to run the query If or Where [table.column]
users.username = 'ExampleUsername' AND users.cardnumber = ''
I don't think I can use NULL instead of '', because its an empty text string?
users.cardnumber = NULL
Anyways, I couldn't come further as this:
INSERT INTO users (cardnumber, hasone)
WHERE users.username = 'ExampleName' AND users.cardnumber = ''
SELECT number, sethasone
FROM cards
WHERE cards.used = '0'
LIMIT 1
I'm a bit of new with SQL, but after I got it right I could put the code into my php script.
-
SOLVED! :
I've used two queries for each column.
update users
set hasone=(select sethasone from cards where used='0' LIMIT 1)
where username='TestUser'
and
update users
set cardnumber=(select number from cards where used='0' LIMIT 1)
where username='TestUser'
then I just deleted the row from cards and I was done.
delete from cards
where used = '1'
LIMIT 1
I gave the user a cardnumber from the table cards and delete that row in cards.
I think you are trying to write a nested query but you didn't know how to write it. If you want to write select query within insert or update query so before doing this Click here to read about sub-query or nested query.
Well, I think that you're trying to re-create a JOIN between 2 table. What you need to do is to add a "card_id" field into the users table. Then to get the user AND the card you can do something like :
SELECT * FROM users u LEFT JOIN cards c ON c.id = u.card_id
I've asked a question regarding my SQL Query a while back and it worked fine until I noticed I had forgotten a very important piece to the code and now I've spent about an hour and a half trying to modify my code. The original question asked a good way to inner join two tables (orders and order_items). I then did a mysql_num_row() over the SQL Query and called it a day. I forgot that there's a cell in my order_items table named quantity. I need to integrate this into my count. I'm including my code below, and any ideas on how to easily implement this would be appreciated.
$SQL2ORDERSEARCH = "
SELECT * FROM order_items
INNER JOIN orders
ON orders.id = order_items.ord
WHERE orders.session = '$sessionID'
AND order_items.dish = '$SEARCHITEMS_OBJECT->dish'
AND order_items.size = 'full'";
$ORDERSEARCH = mysql_query($SQL2ORDERSEARCH) or die(mysql_error());
$ORDERSEARCH_NUM_ROWS = mysql_num_rows($ORDERSEARCH);
$FULLTOTAL = $FULLTOTAL + $ORDERSEARCH_NUM_ROWS;
I tried attempting a different route by doing a count. So I modified my Query as such:
$SQL2ORDERSEARCH = "
SELECT COUNT(quantity) FROM order_items
INNER JOIN orders
ON orders.id = order_items.ord
WHERE orders.session = '$sessionID'
AND order_items.dish = '$SEARCHITEMS_OBJECT->dish'
AND order_items.size = 'full'";
I don't technically need anything else since all I am trying to do is count how many dishes are involved here. I then created an if statement to figure if there was any rows coming from this query, and then calculate the quantity of them.
if($ORDERSEARCH_NUM_ROWS > 0 ) {
while($ORDERSEARCH_OBJECT = mysql_fetch_object($ORDERSEARCH)) {
$FULLQTY = COUNT($ORDERSEARCH_OBJECT->quantity);
}
}
I've gotten mixed results. Sometimes I get just straight 1's down the data table. Other times (depending on any small changes such as $FULLQTY += COUNT($ORDERSEARCH_OBJECT->quantity); and trying that) I get results where it seems almost pattern-like with the numbers increasing by 5+ and somehow adding up to near 20+ the further down the list you go.
I'm just looking for an easy way to get the count of the quantity cell in order_items, displaying them down a table, and then calculating a total. I have everything fine and dandy minus getting the count of the quantity cell in order_items. Any ideas, I'd greatly appreciate it!
Wouldn't a simple select sum(quantity) work here ?
I have two tables, one holds the information of contributors to my site and one holds information on photographs contributed.
For the admin side of the site, I want to create a table using php and mysql that displays all contributors but also counts the number of photographs each contributor has available for the site.
I get the list of names using this code
SELECT *
FROM site_con
ORDER BY surn ASC
I have then set up a loop to list all the names but have added a query within that loop to count the number of photographs using this code
$contributor = $row_rsContrib['con_Code'];
mysql_select_db($database_connGrowl, $connGrowl);
$query_rsCounter = "SELECT COUNT(*) AS Count
FROM site_phts
WHERE photter = $contributor";
$rsCounter = mysql_query($query_rsCounter, $connGrowl) or die(mysql_error());
$row_rsCounter = mysql_fetch_assoc($rsCounter);
$totalRows_rsCounter = mysql_num_rows($rsCounter);
The only problem is when '$contributor' is not in the photographs table, it returns an error.
Any ideas?
You can get the list of contributors & the number of photos in a single query:
SELECT sc.*,
COALESCE(x.numPhotos, 0) AS numPht
FROM SITE_CON sc
LEFT JOIN (SELECT sp.photter,
COUNT(*) AS numPhotos
FROM SITE_PHTS sp
GROUP BY sp.photter) x ON x.photter = sc.con_code
ORDER BY ssc.surn
Your query fails because a photographer doesn't necessarily have contributions -- the query above returns the list of photographers, and those without photos associated will have a numPht value of zero. Here's a primer on JOINs, to help explain the OUTER JOIN that's being used.
Actually the best way to do this is by using MSQL to count rather than PHP:
SELECT site_con.*, COUNT( photo_id )
FROM site_con
LEFT JOIN site_phts ON site_con.con_Code = site_phts.photter
GROUP BY site_con.con_Code
ORDER BY site_con.surn
The LEFT JOIN has the special property of creating NULL entries when there is no row in the right table (photos) that matches a contributor row. COUNT will not count these NULL entries. (You need some unique column in the photos table, I used photo_id for that.)
this is the relation between Contributors and photographs:
1 photograph can have a most 1 Contributor
1 Contributor can have a most infinit photograph
Contributor <-(0,n)------(0,1)-> Photograph
so you might wanna add a connexion betweet those two tables, I mean you add the con_id to the photographs table (as a column).
this way you'll be able to retrieve all the informations in one SQL query.
(like OMG Ponies just said)
Do something like this, I believe this should work :
$result = mysql_query("SELECT COUNT(*) AS Count FROM site_phts WHERE photter = '$contributor'"); // put the single quote if $contributor is a string value
//use mysql_fetch_array
if ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %d", $row[0]);
}
Hopefully this works, Good luck mate !