mysql combine two columns into one in query [closed]
Clash Royale CLAN TAG#URR8PPP
mysql combine two columns into one in query [closed]
select a.id, a.one, a.two,b.three FROM a
left join b
on b.three=a.one and b.three=a.two
group by a.id
Is this query right?
I want b.three
equal a.one
and also a.two
a.one
and a.two
have different values.
b.three
a.one
a.two
a.one
a.two
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Query will not result any row if a.one and a.two is unequal.
– Spidi's Web
Aug 10 at 9:02
Given that a.one and a.two have different values b.three can't match both of them at the same time. So perhaps you want
on b.three=a.one OR b.three=a.two
instead.– Nick
Aug 10 at 9:02
on b.three=a.one OR b.three=a.two
The general
GROUP BY
rule says: "If a GROUP BY clause is specified, each column reference in the SELECT list must either identify a grouping column or be the argument of a set function." (Older MySQL don't care about this, but may return an unpredictable result. Newer MySQL versions are stricter.)– jarlh
Aug 10 at 9:38
GROUP BY
5 Answers
5
Hello As you have mentioned in your question that a.one and a.two have different values and you want to fetch the result where b.three must be equal to a.one and a.two
So your current query will give result only when you have a.one and a.two both same values.
If your data has few similar values for a.one and a.two and that you want to compare with b.three then yes your query is perfectly right but if a.one and a.two will be always different then this query will not work.
If you want to fetch the result where b.three should be either equal to a.one or a.two then you should use Or condition instead of and.
It's better that you first clarify what exactly your requirement is and if possible then you should provide some sample data so others can understand your problem correctly.
I hope this ans will be useful for you.
You have 2 ways of joining by 2 parameters:
SELECT *
FROM t1
JOIN t2 USING (id, date)
or:
JOIN t2 ON (t2.id = t1.id AND t2.date = t1.date)
But the problem is that i haven't made a request with such situation when you need to use 1 table column for both conditions. Just try it and I hope that it'll help you!
i know also if a.one and a.two different values query dont work.
but i want to know how we can combine these columns and then equal to b.three.
it is one part of my real example.if u want i can send main problem.
$con =mysqli_connect('localhost','root','','DB'); if
(!empty($_SERVER['HTTP_CLIENT_IP']))
$ip=$_SERVER['HTTP_CLIENT_IP']; elseif
(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; else
$ip=$_SERVER['REMOTE_ADDR']; $sql ="SELECT kafsent.id, kafsent.home,
COUNT(likes.ip) as like
FROM kafsent
LEFT JOIN likes
ON likes.post=kafsent.id
GROUP BY kafsent.id"; $query=mysqli_query($con,$sql) ; while($row=mysqli_fetch_assoc($query)) $home =$row['home']; $id =$row['id']; $likes =$row['like']; ?> <a href="index.php?id=<?php echo $id; ?> "> <?php echo $home; ?></a> <?php echo $likes; ?>
<?php ?> <?php if(isset($_GET['id'])) $id= (int)$_GET['id']; $query= "INSERT INTO likes(odd_id,ip)
SELECT '$id', '$ip' FROM kafsent WHERE EXISTS( SELECT id FROM test WHERE id='$id') AND NOT EXISTS( SELECT id FROM likes WHERE ip='$ip' AND odd_id='$id' ) LIMIT 1 ";enter image description here $result=mysqli_query($con,$query); header('Location:index.php');
kafsent table
kafsent table data
like table structure
Could you provide some sample data and expect result?
– D-Shih
Aug 10 at 8:56