I have this problem i can't seem to solve. I'm kinda new at programming so it could be really easy but i couldn't find anything. I have a big array through which I loop and if there are duplicates they need to be set to false in a column called address_correct. Here is my code:
$sql1 = "SELECT street, entity_id FROM adresfix";
$arraycorrect = [];
$arrayincorrect = [];
$i = 0;
$db_query = $db->query($sql1);
$adres_rows = $db_query->fetch_all();
var_dump($adres_rows);
foreach ($address_rows as $address) {
if (in_array($address_rows[$i][0] , $arraycorrect)){
echo "Good";
array_push($arrayincorrect, $address_rows[$i][0]);
$sql = "UPDATE adresfix SET address_correct = 'false' WHERE";
} else {
array_push($arraycorrect, $address_rows[$i][0]);
echo "False";
}
$i++;
}
I have no idea what is should put after where in the sql query to make sure only the duplicates are updated. Can anybody help me with this problem?
There are multiple ways to achieve this, but I am not sure what your endgoal is. The most easy thing to do to remove duplicates is create a new table and insert all unique records in that new table, like this:
CREATE TABLE adress_unique LIKE adresfix;
INSERT INTO address_unique
SELECT * FROM adresfix
GROUP BY street,number,city; // Combination of columns that contains duplicate values.
Eventually, after reviewing, you could remove the old table and rename the new one:
DROP TABLE adresfix;
ALTER TABLE address_unique RENAME TO adresfix;
Okay, so the select query to get all duplicate rows, based on multiple columns (assumption), is:
SELECT * FROM adresfix AS a1
INNER JOIN adresfix AS a2
WHERE (
a1.street = a2.street
AND
a1.number = a2.number
AND
a1.city = a2.city
)
Now, if you want to update those rows, you should add this query to the update query, as a subquery or join query. Example:
UPDATE adresfix
SET address_correct = false
WHERE entity_id IN (
SELECT entity_id FROM (
SELECT a1.entity_id
FROM adresfix AS a1
INNER JOIN adresfix a2
WHERE (
a1.street = a2.street
AND
a1.number = a2.number
AND
a1.city = a2.city
)
) AS foo
)
See this fiddle: http://sqlfiddle.com/#!9/8899eb/2
I have an SQL query that fetches posts from a database. Everything works fine, but now I need to order the results by the number of comments each post has. The comments are on a separate table and they have a post_id column that links it to the post. I need to order the posts by the count of the comments table based on a shard ID? I have tried everything but every time I try to add something to my query it stops running completely and leaves my page blank. I need help to know where to put the other JOIN statement. This is my query:
$union = "UNION ALL
(
SELECT DISTINCT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,wallposts.columnTimesShared,
wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
wallposts.url,wallposts.description,wallposts.cur_image,
wallposts.uip,wallposts.likes,wallposts.userid,
wallposts.posted_by,wallposts.post as postdata,wallusers.*,
UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername,
PosterTable.mem_fname as posterFname,PosterTable.work as posterWork,
PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created
FROM
wallusers,wallusers as PosterTable, wallposts
LEFT JOIN walllikes_track
ON wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id."
WHERE
wallusers.active = 1
AND
PosterTable.active = 1
AND
wallposts.group_id IN (".$groups.")
AND
wallposts.group_id != 0
AND
PosterTable.mem_id = wallposts.posted_by
AND
wallposts.marked < ".$this->flagNumber."
AND
wallusers.mem_id = wallposts.posted_by ) ";
The comments table is called wallcomments and it has a column called post_id. I know I need to use JOIN and COUNT but I don't know where to put it within my current code.
Try this query, I didn't run but i updated it.
SELECT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,wallposts.columnTimesShared,
wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
wallposts.url,wallposts.description,wallposts.cur_image,
wallposts.uip,wallposts.likes,wallposts.userid,
wallposts.posted_by,wallposts.post as postdata,wallusers.*,
UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername,
PosterTable.mem_fname as posterFname,PosterTable.work as posterWork,
PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created
FROM
wallusers,wallusers as PosterTable, wallposts
WHERE
wallusers.active = 1
AND
PosterTable.active = 1
AND
wallposts.group_id IN (".$groups.")
AND
wallposts.group_id != 0
AND
PosterTable.mem_id = wallposts.posted_by
AND
wallposts.marked < ".$this->flagNumber."
AND
wallusers.mem_id = wallposts.posted_by ) " AND wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id.";
A more readable query might look like this...
At least then we'd have a chance.
SELECT DISTINCT p.p_id
, p.is_profile_notes
, p.times_viewed
, p.columnTimesShared
, p.marked
, p.secure_id
, p.media...
FROM wallposts p...
This is my query:
SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190
It runs perfectly on MySQL directly but when I run it from my PHP codeigniter model I get the #1054 error. I have no idea why this is happening. Please help.
Here is the PHP code:
$query = $this->db->query("SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid
FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190");
As per your image reference, paypal transaction table contain buyerId and you used it as buyer_id. So use the following.
Use like this
$sql = "select count(distinct(`paypal_transaction`.`buyerId`)) as `cid` from `eg_posts_details` inner join `paypal_transaction` on `paypal_transaction`.`id` = `eg_posts_details`.`OrderId` where `seller_id`= '190' ";
$query = $this->db->query($sql);
Hope its work for you
** Table Inputs**
(id_inpits, name, quantity_inputs)
Table Outputs
`(id_outputs, name_outputs, quantity_outputs, quantity_disponible)
when I want to update the quantity_output, and calculate the quantity_disponbile
knowing that : quantity_disponbile = quantity_inputs - quantity_output
I tried with :
if(!empty($_POST['do'])) {
$m_id = $_POST['id_output'];
$quantity_outputs = $_POST["quantity_outputs"];
$sql = $db->query("UPDATE outputs AS o INNER JOIN inputs As i ON i.id_input = o.inputs_id SET o.quantity_dispo = 'select quantity_inputs from inputs - FROM (select quantity_outputs from outputs )', o.quantity_outputs = '$quantity_outputs' WHERE o.id_output =' $m_id'");
if(!$sql) {
die(mysql_error());
}
}
One suggestion: quantity_disponbile is supposed a calculated field from both table. So split the logic into two: one select query and one update query. The update query just need to update the quantity_output, and the select query can be like:
select
i.quantity_inputs - o.quantity_output = quantity_disponible
from output o
inner join input i on i.id_inpits = o.inputs_id
I don't correct the typo anyway
The following code is used in a query for fetching records. It uses the electors.ID to find the corresponding voting_intention.elector from a second table.
$criteria = "FROM voting_intention,electors WHERE voting_intention.elector = electors.ID AND voting_intention.pledge IN ('C','P') AND electors.postal_vote = 1 AND electors.telephone > 0"
The problem is that some electors will have more than one pledge in the voting_intentions table.
I need it to match only on the latest voting_intention.pledge based on the field votin_intention.date for each elector.
What is the simplest way of implementing that.
The rest of the code:
function get_elector_phone($criteria){
$the_elector = mysql_query("SELECT * $criteria ORDER BY electors.ID ASC"); while($row = mysql_fetch_array($the_elector)) {
echo $row['ID'].','; }
}
You could use a sub-select with the MAX() function. Add the following into your WHERE clause.
AND voting_intention.date = (select MAX(date)
from voting_intention vote2
where voting_intention.elector = vote2.elector)
Here is a SQLFiddle of the results.
So pretty much, you only want to bother looking at the most recent row that fits the first two criteria in your code. In that case, you would want to filter out the voting_intention table beforehand to only have to worry about the most recent entries of each. There's a question/answer that shows how do do that here.
Try selecting the following instead of voting_intention (from the answer of the linked question, some table and field names replaced):
SELECT voting_intention.*
FROM voting_intention
INNER JOIN
(
SELECT elector, MAX(date) AS MaxDate
FROM voting_intention
GROUP BY elector
) groupedintention ON voting_intention.elector = groupedintention.elector
AND voting_intention.date = groupedintention .MaxDate
Question url: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?